两个无关线程的运行结果互相包含对方内容是怎么回事?代码如下:
public class NumberThread extends Thread {
private int first;
public NumberThread(String name, int first) {
super(name);
this.first = first;
}
public NumberThread(String name) {
this(name, 0);
}
public void run() {
System.out.print("\n"+this.getName()+": ");
for(int i=first;i<50;i+=2) {
System.out.print(i+" ");
}
System.out.print(this.getName()+"Finish!");
}
public static void main(String args[]) {
System.out.println("Current thread ="+Thread.currentThread().getName());
NumberThread thread_odd = new NumberThread("奇数线程", 1);
NumberThread thread_even = new NumberThread("偶数线程", 2);
thread_even.start();
thread_odd.start();
System.out.println("activityCount="+thread_even.activeCount());
}
}
运行结果:
Current thread =main
activityCount=3
偶数线程: 2 4 6
奇数线程: 8 1 10 3 5 7 9 11 13 15 17 19 21 12 23 14 25 16 27 18 29 20 31 22 33 24 35 26 37 28 39 30 41 32 43 34 45 36 47 38 49 40 奇数线程Finish!42 44 46 48 偶数线程Finish!
守候你守候我
收到一只叮咚
相关分类