CodePudding user response:
Public class Demo {
Private volatile int flag=0;
Public void runThread () {
Thread the t1=new Thread (new Thread1 ());
Thread t2=new Thread (new Thread2 ());
t1.start();
t2.start();
}
Public class Thread1 implements Runnable {
Public void the run () {
int i=1;
While (I & lt; {
=100)If (flag==0) {
System. The out. Println (" thread 1="+ I);
I +=2;
Flag=1;
}
}
}
}
Public class Thread2 implements Runnable {
Public void the run () {
Int I=2;
While (I & lt; {
=100)If (flag==1) {
System. The out. Println (" thread 2="+ I);
I +=2;
Flag=0;
}
}
}
}
Public static void main (String [] args) {
The new Demo (). RunThread ();
}
}