黑马程序员技术交流社区
标题:
多生产者多消费者小问题
[打印本页]
作者:
李培根
时间:
2012-12-5 13:23
标题:
多生产者多消费者小问题
class Resource2{
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name){
while(flag)
try{this.wait();}
catch(InterruptedException e){}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者"+this.name);
flag = true;
notifyAll();
}
public synchronized void get(){
while(!flag)
try{this.wait();}
catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);
flag = false;
notifyAll();
}
}
class Producer implements Runnable{
Resource2 r;
Producer(Resource2 r){
this.r = r;
}
public void run(){
while(true){
r.set("烤鸭");
}
}
}
class Consumer implements Runnable{
Resource2 r;
Consumer(Resource2 r){
this.r = r;
}
public void run(){
while(true){
r.get();
}
}
}
public class ConsumerDemo2 {
public static void main(String[] args) {
Resource2 r = new Resource2();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t0 = new Thread(pro);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(con);
Thread t3 = new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
打印结果
Thread-1...生产者烤鸭35518
Thread-3...消费者...烤鸭35518
Thread-0...生产者烤鸭35519
Thread-2...消费者...烤鸭35519
Thread-1...生产者烤鸭35520
Thread-3...消费者...烤鸭35520
Thread-0...生产者烤鸭35521
Thread-2...消费者...烤鸭35521
Thread-1...生产者烤鸭35522
Thread-3...消费者...烤鸭35522
别的都正常就是烤鸭不能从1开始编号。每次开始的编号都不同,
开始以为是内存中的count的问题,经过验证开始的编号无规律,
有的时候6万,有的时候3万,不是一直增加,求开导。先感谢了
作者:
郑传庆
时间:
2012-12-5 13:32
我改了一下,你看是不是你想要的结果
class Resource2 {
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name) {
while (flag)
try {
this.wait();
} catch (InterruptedException e) {
}
this.name = name + count;
count++;
System.out.println(Thread.currentThread().getName() + "...生产者"
+ this.name);
flag = true;
// notifyAll();
notify();
}
public synchronized void get() {
while (!flag)
try {
this.wait();
} catch (InterruptedException e) {
}
System.out.println(Thread.currentThread().getName() + "...消费者..."
+ this.name);
flag = false;
// notifyAll();
notify();//唤醒等待的线程
}
}
class Producer implements Runnable {
Resource2 r;
Producer(Resource2 r) {
this.r = r;
}
public void run() {
while (true) {
r.set("烤鸭");
}
}
}
class Consumer implements Runnable {
Resource2 r;
Consumer(Resource2 r) {
this.r = r;
}
public void run() {
while (true) {
r.get();
}
}
}
public class ConsumerDemo2 {
public static void main(String[] args) {
Resource2 r = new Resource2();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t0 = new Thread(pro);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(con);
Thread t3 = new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
1.jpg
(57.99 KB, 下载次数: 7)
下载附件
2012-12-5 13:32 上传
作者:
李培根
时间:
2012-12-6 07:41
郑传庆 发表于 2012-12-5 13:32
我改了一下,你看是不是你想要的结果
非常感谢,可是把notifyAll();改成notify();会出现线程的安全问题的啊,会出现死锁情况。
作者:
洪灿阳
时间:
2012-12-6 10:39
lz说的没错,用notify会导致死锁,我觉得可能是你传递的name出现问题了,感觉name的获取和表达有点问题,就是this.name=name+count这个地方。你改改应该就没问题了。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2