【发布时间】:2013-04-30 15:51:47
【问题描述】:
我有三星 Galaxy S3,它使用自己的 Exynos 4 Quad 处理器。 所以我想优化我的应用,让它可以使用所有 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 中运行任务。和处理时间 - 仍然是 8 秒。
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 }});
看起来所有工作都是同步完成的。为什么不并行?
【问题讨论】:
-
没有真正的项目,而不是像这样不完整的样本,是不可能告诉你的。也许所有四个都在争夺某些资源,例如磁盘 I/O,这会降低 CPU 并行性的效率。
标签: android multithreading multicore