黑马程序员技术交流社区
标题:
消费者生产者问题中的同步,发生了全部wait()的情况!!!
[打印本页]
作者:
茄子
时间:
2014-6-9 22:53
标题:
消费者生产者问题中的同步,发生了全部wait()的情况!!!
本帖最后由 茄子 于 2014-6-9 23:15 编辑
马儿们,茄子又来献丑了,今天是生产者与消费者同步问题发生全部线程等待的情况,
自己看了半天,还是没有找出原因,这是为什么呢??
下面是代码:
class Resource
{
private String name;
private int count=1;
private boolean flag=false;
public synchronized void set(String name)
{
while(flag)
try{this.wait();}catch(Exception e){}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+name+"……生产者……"+this.name);
flag=true;
this.notifyAll();
}
public synchronized void out()
{
while(!flag)
try{this.wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+name+"……………消费者……………"+this.name);
flag=false;
this.notifyAll();
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res=res;
}
public void run()
{
res.set("商品");
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res) //自定义构造函数
{
this.res=res;
}
public void run()
{
res.out();
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource res=new Resource();
Producer p=new Producer(res);
Consumer c=new Consumer(res);
Thread t1=new Thread(p); //2个线程同时生产,
Thread t2=new Thread(p);
Thread t3=new Thread(c); //2个线程负责消费,
Thread t4=new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
运行结果是:
Thread-0商品……生产者……商品1
Thread-3商品1……………消费者……………商品1
Thread-1商品……生产者……商品2
Thread-2商品2……………消费者……………商品2
这是为什么呢????
作者:
轩辕冰晨
时间:
2014-6-9 23:02
你只是运行一次,所以只打印4条,你要测多线程,应该在run()方法中加入循环
作者:
轩辕冰晨
时间:
2014-6-9 23:12
package org.test;
class Resource {
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name) {
while (flag)
try {
this.wait();
} catch (Exception e) {
}
this.name = name + count++;
System.out.println(Thread.currentThread().getName() + name + "……生产者……"
+ this.name);
flag = true;
this.notifyAll();
}
public synchronized void out() {
while (!flag)
try {
this.wait();
} catch (Exception e) {
}
System.out.println(Thread.currentThread().getName() + name
+ "……………消费者……………" + this.name);
flag = false;
this.notifyAll();
}
}
class Producer implements Runnable {
private Resource res;
Producer(Resource res) {
this.res = res;
}
public void run() {
while(true)
res.set("商品");
}
}
class Consumer implements Runnable {
private Resource res;
Consumer(Resource res) // 自定义构造函数
{
this.res = res;
}
public void run() {
while(true)
res.out();
}
}
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource res = new Resource();
Producer p = new Producer(res);
Consumer c = new Consumer(res);
Thread t1 = new Thread(p); // 2个线程同时生产,
Thread t2 = new Thread(p);
Thread t3 = new Thread(c); // 2个线程负责消费,
Thread t4 = new Thread(c);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Thread-0商品……生产者……商品52607
Thread-3商品52607……………消费者……………商品52607
Thread-1商品……生产者……商品52608
Thread-2商品52608……………消费者……………商品52608
Thread-0商品……生产者……商品52609
Thread-3商品52609……………消费者……………商品52609
Thread-1商品……生产者……商品52610
Thread-2商品52610……………消费者……………商品52610
Thread-0商品……生产者……商品52611
Thread-3商品52611……………消费者……………商品52611
Thread-1商品……生产者……商品52612
Thread-2商品52612……………消费者……………商品52612
Thread-0商品……生产者……商品52613
Thread-3商品52613……………消费者……………商品52613
Thread-1商品……生产者……商品52614
Thread-2商品52614……………消费者……………商品52614
Thread-0商品……生产者……商品52615
Thread-3商品52615……………消费者……………商品52615
Thread-1商品……生产者……商品52616
Thread-2商品52616……………消费者……………商品52616
Thread-0商品……生产者……商品52617
Thread-3商品52617……………消费者……………商品52617
Thread-1商品……生产者……商品52618
Thread-2商品52618……………消费者……………商品52618
Thread-0商品……生产者……商品52619
Thread-3商品52619……………消费者……………商品52619
Thread-1商品……生产者……商品52620
Thread-2商品52620……………消费者……………商品52620
作者:
茄子
时间:
2014-6-9 23:14
轩辕冰晨 发表于 2014-6-9 23:02
你只是运行一次,所以只打印4条,你要测多线程,应该在run()方法中加入循环
谢谢兄弟!!!受教了!
作者:
轩辕冰晨
时间:
2014-6-9 23:40
茄子 发表于 2014-6-9 23:14
谢谢兄弟!!!受教了!
哈,不用客气...
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2