生产者 public class Producer implements Runnable { private Info info = null; public Producer(Info info){ this.info = info; } @Override public void run() { boolean flag = false; for(int i=0;i<50;i++){ if(!flag){ this.info.set("xielongtao", "basketball"); flag = true; }else{ this.info.set("谢龙涛","篮球"); flag = false; } }
}
}消费者 public class Consumer implements Runnable{ private Info info; public Consumer(Info info){ this.info = info; } @Override public void run() { for(int i=0;i<50;i++){ this.info.get(); } }}信息类 public class Info { private String name = "谢龙涛"; private String hobby = "篮球"; private boolean flag = false; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } public synchronized void set(String name,String hobby){ System.out.println(Thread.currentThread().getName()+"set"); if (!flag) {// 表示是满的 不能加 try { this.wait(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } this.setName(name); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.setHobby(hobby); flag = false;//让他取 super.notify(); } } public synchronized void get(){ System.out.println(Thread.currentThread().getName()+"get"); if (flag) {//表示是空的 不能取 try { this.wait(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("name:" + this.getName() + " hobby:" + this.getHobby()); flag = true;// 让他放 super.notify(); } }
}main方法 public class SycronizeTest { public static void main(String[] args) { Info info = new Info(); Consumer c = new Consumer(info); Producer p = new Producer(info); new Thread(c,"consumer").start(); new Thread(p,"producer").start(); } } 求助啊 大神们~~~~~~~~~
public class Producer implements Runnable {
private Info info = null;
public Producer(Info info){
this.info = info;
}
@Override
public void run() {
boolean flag = false;
for(int i=0;i<50;i++){
if(!flag){
this.info.set("xielongtao", "basketball");
flag = true;
}else{
this.info.set("谢龙涛","篮球");
flag = false;
}
}
}
}消费者
public class Consumer implements Runnable{
private Info info;
public Consumer(Info info){
this.info = info;
}
@Override
public void run() {
for(int i=0;i<50;i++){
this.info.get();
}
}}信息类
public class Info {
private String name = "谢龙涛";
private String hobby = "篮球";
private boolean flag = false;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public synchronized void set(String name,String hobby){
System.out.println(Thread.currentThread().getName()+"set");
if (!flag) {// 表示是满的 不能加
try {
this.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.setName(name);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setHobby(hobby);
flag = false;//让他取
super.notify();
}
}
public synchronized void get(){
System.out.println(Thread.currentThread().getName()+"get");
if (flag) {//表示是空的 不能取
try {
this.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("name:" + this.getName() + " hobby:"
+ this.getHobby());
flag = true;// 让他放
super.notify();
}
}
}main方法
public class SycronizeTest {
public static void main(String[] args) {
Info info = new Info();
Consumer c = new Consumer(info);
Producer p = new Producer(info);
new Thread(c,"consumer").start();
new Thread(p,"producer").start();
}
}
求助啊 大神们~~~~~~~~~
xielongtao basketball
谢龙涛 篮球
可是运行main方法没有输出这是为什么? 死锁了吗 两个线程都在跑 有唤醒啊消费者为什么没有取到?
consumer线程启动,循环调用info.get(),进不去if{},没机会nofity()
producer线程启动,调用info.set(),进入if{},马上就wait()了,没机会修改flag
问题出在set 和get 方法里,以get方法为例,if语句有问题,假设消费线程得到锁,获得运行机会,同时flag 为false 表示可以取,但你的代码什么也没干,方法就结束了。所以这个地方有问题。所以要把可以消费的执行代码放在if语句块后边,不能放在if语句块里面,放在里面的话,只有flag为true时,进入等待,生产线程结束生产,唤醒等待线程时,才能运行。
所以代码需要稍微改一下: public synchronized void set(String name,String hobby){
System.out.println(Thread.currentThread().getName()+"set");
if (!flag) {// 表示是满的 不能加
try {
this.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.setName(name);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//-------------------------------if 在此结束
this.setHobby(hobby);
flag = false;//让他取
super.notify();
//}// -------------------------------这不要
}
public synchronized void get(){
System.out.println(Thread.currentThread().getName()+"get");
if (flag) {//表示是空的 不能取
try {
this.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//----------------------------------------if 在此结束
System.out.println("name:" + this.getName() + " hobby:"
+ this.getHobby());
flag = true;// 让他放
super.notify();
//}//------------------------------------- 这不要
}
另外,最好用while 替代if,更安全。