package thread;
class QQ{
private String name;
private String sex;
public synchronized void put(String name,String sex){
this.name=name;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sex=sex;
}
public synchronized void get(){
System.out.println(name +"----------->"+sex);
}
}
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();
}
}