package day12;
class Rec2 {
private String name ;
private String sex;
private boolean flag;
public synchronized void set(String name,String sex){
if(this.flag)
try {this.wait();} catch (InterruptedException e) {}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void get(){
if(!this.flag)
try {this.wait();} catch (InterruptedException e) {}
System.out.println(this.name+"......."+this.sex);
flag = false;
this.notify();
}
}
class Input2 implements Runnable{
private Rec2 r;
Input2(Rec2 r){
this.r= r;
}
public void run(){
int x = 0;
while(true){
if(x==0)
r.set("zhangsan", "man");
else
r.set("李四", "女女女");
x = (x+1)%2;
}
}
}
class Output2 implements Runnable{
private Rec2 r;
Output2(Rec2 r){
this.r = r;
}
public void run(){
while(true){
r.get();
}
}
}
public class InputOutputDemo2 {
public static void main(String args[]){
Rec2 r = new Rec2();
new Thread(new Input2(r)).start();
new Thread(new Output2(r)).start();
}
}
|
|