讨论一个多线程问题

CanadianTire 2009-12-09 01:38:14
很简单的生产消费者例子,我想问一下,在类FactoryStock中
方法getProduct()里的notifyAll()我注释上和取消注释效果一样,至少我运行很久没有发现死锁,这行代码到底是否需要加在这里呢?请指教。
见代码:
1。

public class FactoryStock
{
public String message="";
public boolean hasStock = false;

public synchronized String getProduct()
{
while(!hasStock)
{
try
{
System.out.println("Waiting in consumer");
wait();
Thread.sleep(2000);
}
catch(InterruptedException e)
{

}
}
hasStock = false;
//notifyAll();
return this.message;
}

public synchronized void putProduct(String productName)
{
while(hasStock)
{
try
{
System.out.println("Waiting in producer "+productName);
wait();
Thread.sleep(2000);
}
catch(InterruptedException e)
{

}
}
hasStock = true;
this.message = "This is product:"+productName;
notifyAll();
}
}

2。

public class ProducerThread implements Runnable
{
private FactoryStock stock;
private String producerName;

public ProducerThread(String producerName,FactoryStock stock)
{
this.stock = stock;
this.producerName = producerName;
}

public void run()
{
// TODO Auto-generated method stub
int i=1;
while(true)
{
stock.putProduct("Product name:"+producerName+"["+i+++"]");
System.out.println("Producer "+producerName+" put product:"+i);
}
}

}

3。

class consumerThread implements Runnable
{
private FactoryStock stock;
private String consumerName;

public consumerThread(String consumerName,FactoryStock stock)
{
this.stock = stock;
this.consumerName=consumerName;
}

public void run()
{
while(true)
{
System.out.println("Consumer "+consumerName+" get product:"+stock.getProduct());
}
}
}

4。

public class TestFactory
{

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
FactoryStock stock = new FactoryStock();
Thread consumer1 = new Thread(new consumerThread("Consumer 1",stock));
Thread consumer2 = new Thread(new consumerThread("Consumer 2",stock));
Thread consumer3 = new Thread(new consumerThread("Consumer 3",stock));
Thread consumer4 = new Thread(new consumerThread("Consumer 4",stock));
Thread consumer5 = new Thread(new consumerThread("Consumer 5",stock));
Thread consumer6 = new Thread(new consumerThread("Consumer 6",stock));
Thread consumer7 = new Thread(new consumerThread("Consumer 7",stock));
Thread consumer8 = new Thread(new consumerThread("Consumer 8",stock));
Thread consumer9 = new Thread(new consumerThread("Consumer 9",stock));
Thread consumer10 = new Thread(new consumerThread("Consumer 10",stock));
Thread producer1 = new Thread(new ProducerThread("Producer 1",stock));
Thread producer2 = new Thread(new ProducerThread("Producer 2",stock));
Thread producer3 = new Thread(new ProducerThread("Producer 3",stock));
Thread producer4 = new Thread(new ProducerThread("Producer 4",stock));
Thread producer5 = new Thread(new ProducerThread("Producer 5",stock));
Thread producer6 = new Thread(new ProducerThread("Producer 6",stock));
Thread producer7 = new Thread(new ProducerThread("Producer 7",stock));
Thread producer8 = new Thread(new ProducerThread("Producer 8",stock));
Thread producer9 = new Thread(new ProducerThread("Producer 9",stock));
Thread producer10 = new Thread(new ProducerThread("Producer 10",stock));
Thread producer11 = new Thread(new ProducerThread("Producer 11",stock));
Thread producer12 = new Thread(new ProducerThread("Producer 12",stock));
Thread producer13 = new Thread(new ProducerThread("Producer 13",stock));
Thread producer14 = new Thread(new ProducerThread("Producer 14",stock));
Thread producer15 = new Thread(new ProducerThread("Producer 15",stock));
Thread producer16 = new Thread(new ProducerThread("Producer 16",stock));
consumer1.start();
consumer2.start();
consumer3.start();
consumer4.start();
consumer5.start();
consumer6.start();
consumer7.start();
consumer8.start();
consumer9.start();
consumer10.start();
producer1.start();
producer2.start();
producer3.start();
producer4.start();
producer5.start();
producer6.start();
producer7.start();
producer8.start();
producer9.start();
producer10.start();
producer11.start();
producer12.start();
producer13.start();
producer14.start();
producer15.start();
producer16.start();
}

}


...全文
114 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ordinance_tiger 2009-12-10
  • 打赏
  • 举报
回复
我是新手
CanadianTire 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 javaalpha 的回复:]
楼主 怎么new了这么多啊。可以使用一个for循环实现。
[/Quote]
呵呵,因为想不通,比较无聊
CanadianTire 2009-12-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xierangh 的回复:]
看了下你程序里有两个 notifyAll();你把他们都注释掉就要锁住了。getProduct()和
putProduct()
[/Quote]
这个我知道,去掉两个就成功死锁。
现在看来是只要保留一个就可以?
JavaAlpha 2009-12-09
  • 打赏
  • 举报
回复
楼主 怎么new了这么多啊。可以使用一个for循环实现。
xierangh 2009-12-09
  • 打赏
  • 举报
回复
看了下你程序里有两个 notifyAll();你把他们都注释掉就要锁住了。getProduct()和
putProduct()
aheadspring 2009-12-09
  • 打赏
  • 举报
回复
程序结构没看懂。
keeya0416 2009-12-09
  • 打赏
  • 举报
回复
我感觉加不加都一样.
你这里用的是wait,是一个释放对象锁的操作.
所以不会发生死锁.
silencexk212112 2009-12-09
  • 打赏
  • 举报
回复
正在学习....

50,550

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧