讨论一个多线程问题

CanadianTire CanadianTire 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
很简单的生产消费者例子,我想问一下,在类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();
}

}


...全文

相关推荐

一个多线程的socket服务器源代码

在我们的示例中,多线程被用来处理来自不同客户端的连接请求,这样服务器就能同时为多个客户提供服务,显著提高了服务效率。接下来,我们讨论Socket。

wutumaaa

python多线程讲解

python多任务之线程一、线程的定义二、查看线程的创建和运行过程三、多线程共享全局变量四、解决共享全局变量出现资源竞争--互斥锁五、创建互斥锁出现死锁的情况六、多线程udp聊天案例七、总结 一、线程的定义 在python中使用threading中的Thread模块来定义使用线程 使用方法如下:"""第一个线程的多任务demo""" import threading import time def sing(): for i in range(5): print('-----s

V-Sugar

python实现爬虫统计学校BBS男女比例之多线程爬虫(二)

本文主要介绍了如何使用Python实现一个多线程爬虫来统计学校BBS上的男女比例,并针对可能遇到的数据问题进行了数据分类和错误处理。文章分为两部分,本部分为第二篇,主要讨论数据分类和如何实现不间断的

weixin_38592420

WPF 已知问题 传入错误数据给到 WriteableBitmap 可能导致渲染线程锁住.rar

然而,如果不正确地使用,WriteableBitmap可能导致线程安全问题,特别是当在一个多线程环境中更新它的像素数据时。

醒目目

python time.sleep()是睡眠线程还是进程

**问题背景:**根据给定文件的标题,“python time.sleep() 是睡眠线程还是进程”,我们可以理解为这个问题是在询问 `time.sleep()` 函数是会让整个进程暂停,还是仅仅阻塞当前线程

weixin_38562079
评论 8
收藏 已收藏
分享
返回顶部
CanadianTire
Java 51,407 成员 86,130 内容