标题: 一个Thread类型存入Thread对象,主方法中调getName(),why输出main? [打印本页] 作者: 13542010298 时间: 2015-11-20 22:50 标题: 一个Thread类型存入Thread对象,主方法中调getName(),why输出main? 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);
}
}