我有一部三星Galaxy S3手机,其使用的是自己的Exynos 4 Quad处理器。
因此,我想要优化我的应用程序,使其能够利用处理器的全部四个核心。
于是我进行了一些测试:
Run task in one thread. Processing time - 8 seconds.
Run task in four threads. Processing time - still 8 sec.
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();
Run task in ExecutorService. And processing time - still 8 sec.
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 }});
看起来所有的工作都是同步完成的。为什么不并行呢?