求解,求解 关于 multithreat
public class ThreatDemo{
public ThreatDemo(){
}
public static void main(String args[]){
Runnable printA = new PrintChar('A', 100);
Runnable printB = new PrintChar('B', 100);
Runnable printC = new PrintChar('C', 100);
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(printC);
thread1.start();
thread2.start();
thread3.start();
}
}
class PrintChar implements Runnable{
private int time;
private char ch;
public PrintChar(char ch1,int time1){
this.time = time1;
this.ch = ch1;
}
public PrintChar(){
}
public void run(){
for(int i=0;i<time;i++)
System.out.print(ch);
}
}
用上面的代码,debug出结果怎么不会是 ABCABCABC... 而是AAAAAAABBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCBBBBBBBBBBBBBAAAAAAAAAAAA...
这样的形式?
------解决方案--------------------为什么要是 ABCABCABC,三个线程又没有进行同步,理论上推进顺序是随机的。debug设置断点的话,更容易执行成AA……BB……CC……的。
------解决方案--------------------线程的输出带有随机性,一次和一次都不一样. 这正是线程的特点.
------解决方案--------------------线程之间是互斥的‘