我正在尝试使用多线程进行一些计算。最多我将不得不进行100次不同的计算。即使我每次尝试平衡10个线程的开始,我都会在Log Cat上挂起线程消息。我认为这会扼杀性能。当我刚推出10个线程时,性能很好。
//launch thread for each accuracy values
executor = Executors.newFixedThreadPool(100/accuracyInterval);
int countThreads = 0;
for(Integer acc: listAccuracies){
Runnable worker = new MyRunnable(acc);
if(accuracyInterval != 10) {
if (countThreads == 10) {
while (threadsDoneCounter < 10) {
//Log.d("THREADS ","WAITING");
}
countThreads = 0;
}
}
executor.execute(worker);
countThreads ++;
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
现在是MyRunnable的代码
public class MyRunnable implements Runnable {
private final int accuracy;
MyRunnable(int accuracy) {
this.accuracy = accuracy;
}
@Override
public void run() {
Log.d("THREAD " , Integer.toString(accuracy) + " START");
try {
calculatePrediction(accuracy);
} catch (Exception e) {
}
threadsDoneCounter++;
Log.d("THREAD " , Integer.toString(accuracy) + " END");
}
}
当我必须启动100个线程时,可以做什么让它有更好的性能?我做错了什么?
感谢您的关注, 此致