我有一个简单的线程测试:
package Thread;
class Sum extends Thread {
int low, up, S;
public Sum(int a, int b) {
low = a;
up = b;
S = 0;
System.out.println("This is Thread " + this.getId());
}
@Override
public void run() {
for (int i = low; i < up; i++) {
S += i;
}
System.out.println(this.getId() + ":" + S);
}
}
public class Tester {
public static void main(String agrs[]) {
Sum T1 = new Sum(1, 100);
T1.start();
Sum T2 = new Sum(10, 100);
T2.start();
System.out.println("Main process terminated");
}
}
但我不明白什么时候执行 run() 方法,它返回相同:
This is Thread 8
This is Thread 9
Main process terminated
9:4905
8:4950
这意味着 run() 方法是在 T1 和 T2 启动之后执行的。我仍然认为当 start() 方法被调用时 run() 将被执行。感谢提前!