编写10个线程,第一个线程从 1 加到 10,第二个线程从 11 加到 20...第十个线程从 91 加到 100, 最后再把十个线程结果相加。
public class Accumulator extends Thread {
public static int sum;
private int stratNum;
public Accumulator(int startNum) {
this.stratNum = startNum;
}
public static synchronized void add(int num) {
sum += num;
}
public void run() {
int sum = 0;
for (int i=0; i<10; i++) {
sum += (stratNum + i);
}
add(sum);
}
public static void main(String[] args) throws Exception {
Thread[] threadList = new Thread[10];
for (int i=0; i<10; i++) {
threadList[i] = new Accumulator((10 * i) + 1);
threadList[i].start();
}
for (int i=0; i<10; i++) {
threadList[i].join();
}
System.out.println("Sum is : " + sum);
}
}