package homework;
class Accumulator extends Thread {
private int stratNum;
public static int sum;
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;
}
//System.out.println(this.currentThread().getName());
add(sum);
}
}
public class Example6_4{
public static void main(String[] args) throws Exception {
Thread[] threadList = new Thread[10];
for (int i = 0; i < 10; i++) {
threadList = new Accumulator(10 * i + 1);
threadList.start();
System.out.println(threadList.currentThread().getName());
}
for (int i = 0; i < 10; i++) {
threadList.join();
//System.out.println(threadList.currentThread().getName());
}
System.out.println("Sum is : " + Accumulator.sum);
}
}
红字部分代码为什么输出的是main,而不是当前所创建线程的默认名字?跪求各位大神解答,谢谢!!
|
|