我已经实现了以下代码类:
public class sumThread implements Runnable {
private int index;
private static int sum = 0;
public sumThread(int index) {
this.index = index;
}
public static int getSum() {
return sum;
}
@Override
public void run() {
for (int i = 0; i < index + 100; i++) {
sum += i;
}
}
}
第2类:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SigRuntime {
public static void main(String args[]) {
ExecutorService es = Executors.newCachedThreadPool();
int index = 0;
// for (int i = 0; i < 10; i++) {
sumThread t = new sumThread(index);
index += 100;
es.execute(t);
// }
System.out.println(sumThread.getSum());
}}
我正在尝试使用10个线程从0到100求和,因为每个线程取100个值
每当我使用sumThread.getSum()时,我都会一直得到0作为返回值!它出什么问题了?
梵蒂冈之花
相关分类