Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 多核处理器的多线程_Android_Multithreading_Multicore - Fatal编程技术网

Android 多核处理器的多线程

Android 多核处理器的多线程,android,multithreading,multicore,Android,Multithreading,Multicore,我有三星Galaxy S3,它使用自己的Exynos 4四处理器。 所以我想优化我的应用程序,使它能够使用处理器的所有4个核心 所以我做了一些测试: 在一个线程中运行任务。处理时间-8秒 在四个线程中运行任务。处理时间-仍为8秒 new Thread() { public void run() { // do 1/4 of task } }.start(); new Thread() { public void run() {

我有三星Galaxy S3,它使用自己的Exynos 4四处理器。 所以我想优化我的应用程序,使它能够使用处理器的所有4个核心

所以我做了一些测试:

  • 在一个线程中运行任务。处理时间-8秒

  • 在四个线程中运行任务。处理时间-仍为8秒

     new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    ExecutorService threadPool = null;
    threadPool = Executors.newFixedThreadPool(4);
    
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    
  • 在ExecutorService中运行任务。和处理时间-仍然是8秒

     new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    new Thread() {
        public void run() {
                        // do 1/4 of task
        }
    }.start();
    
    ExecutorService threadPool = null;
    threadPool = Executors.newFixedThreadPool(4);
    
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    threadPool.execute(new Runnable() {
        @Override
        public void run() {
             // do 1/4 of task
                          }});
    

  • 看起来所有的工作都是同步完成的。为什么它不是并行的?

    您可以用C或Java尝试我的测试:


    当MultiCore.java在Nexus 4上运行时,我看到:

    Thread 1 finished in 1516ms (1267234688)
    Thread 3 finished in 1494ms (1519485328)
    Thread 0 finished in 1530ms (51099776)
    Thread 2 finished in 1543ms (-1106614992)
    All threads finished in 1550ms
    
    更新:
    使用systrace观看测试非常有用。作为回答类似问题的一部分,我为这个测试设置了一个页面。您可以看到线程是如何调度的,并可以看到其他两个内核的运行。

    如果没有一个真正的项目,而不是像这样不完整的示例,就不可能告诉您。也许这四种方法都在某些资源上竞争,比如磁盘I/O,这会降低CPU并行性的效率。