Home > Back-end >  Run "li si - 12" will only appear when the results, seek guidance problems
Run "li si - 12" will only appear when the results, seek guidance problems

Time:11-15

The class People {
private String name;
private int age;
Private Boolean flag=false;
Public synchronized void set (String name, int age) {
If (flag) {
Try {
This. Wait ();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
this.name=name;
this.age=age;
flag=true;
This. Notify ();
}
Public synchronized void out () {
if(! Flag) {
Try {
This. Wait ();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
System. The out. Println (name + "--" + age);
flag=false;
This. Notify ();
}
}
The class my_In implements Runnable {
People p1.
My_In (People (p1) {
This. P1=p;
}
Public void the run () {
int y=0;
While (true) {
If (y==0) {
P1. The set (" bill ", 12);
} else {
P1. The set (" zhang ", 21);
}
Y=(y + 1) % 2;
//p1. Set (" bill ", 12);
}
}
}
The class my_Out implements Runnable {
People p1.
My_Out (People (p1) {
This. P1=p;
}
Public void the run () {
P1. Out ();
}
}
Public class WNDemo {
Public static void main (String [] args) {
People p=new People ();
My_In m1=new my_In (p);
My_Out m2=new my_Out (p);
Thread the t1=new Thread (m1);
Thread t2=new Thread (m2);
T1. Start ();
T2. Start ();
}
}

CodePudding user response:



You print the thread of execution is equivalent to refer to carried out once, is gone, only print a bill for certain, try look at the picture
  • Related