生产者消费者--Info.java

package org.shw.pc;

public class Info {
private String title="李兴华";
private String content="java讲师";
private boolean flag=false; //false--表示可以取走,但是不能生产 true--表示可以生产,但是不能取走

public synchronized void set(String title,String content){
if(flag==false){//已经生产过了,需要等待
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.setTitle(title);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.setContent(content);
this.flag=false;
super.notify();
}

public synchronized void get(){
if(flag==true){ //表示不能取
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.title+" -->"+this.content);
this.flag=true;
super.notify();
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

}
posted @ 2012-03-19 21:55  haiwei.sun  阅读(227)  评论(0编辑  收藏  举报
返回顶部