package thread;
class QQ{
private String name;
private String sex;
boolean flag=false;
public synchronized void put(String name,String sex){
if(flag)
try {
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
this.name=name;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sex=sex;
flag=true;
notify();
}
public synchronized void get(){
if(!flag)
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name +"----------->"+sex);
flag=false;
notify();
}
}
class Producer1 implements Runnable{
QQ qq=null;
public Producer1(QQ qq){
this.qq=qq;
}
public void run() {
int i=0;
while(true){
if(i==0){
qq.put("zxx", "nan");
}else{
qq.put("cq", "nv");
}
i=(i+1)%2;
}
}
}
class Consumer1 implements Runnable{
QQ qq=null;
public Consumer1(QQ qq){
this.qq=qq;
}
public void run() {
while(true){
qq.get();
}
}
}
public class ThreadCommunciation {
public static void main(String[] args) {
QQ qq=new QQ();
new Thread(new Producer1(qq )).start();
new Thread(new Consumer1(qq)).start();
}
}