在生产者/消费者模型中,生产者Producer负责生产数据,而消费者Consumer负责使用数据。多个生产者线程会在同一时间运行,生产数据,并放到内存中一个共享的区域。期间,多个消费者线程读取内存共享区,消费里面的数据。
要求:
1. 针对上面的场景,请创建2个类,一个叫Producer,一个叫Consumer.
2. Producer类继承Thread类,并实现把数据放到内存共享区的功能,这个功能要求是线程安全的。在个Producer类中的run方法中,循环20次,每次把一个整数放到内存共享区中。
3. Consumer类也继承Thread类,并实现在没有冲突的情况下,从内存共享区中获取数据,并在标准输出设备中打印输出。输出的格式为:Consumer thread X retrieved integer Y.
4. 最后,创建一个main class,创建10个Procuder线程和4个消费者线程并启动这些线程。
5. 要求有效代码行数尽量少,最好不超过100行。
我大概的做了一下,以下是我的代码实现:
一. 消费者类
/**
* 消费者类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
class Consumer extends Thread {
private Conn conn;
private int consumerNumber;
public Consumer(Conn conn1, int conNum) {
conn = conn1;
consumerNumber = conNum;
}
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("Consumer thread " + consumerNumber + " retrieved integer: " + conn.read()); //
try {
sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
* 消费者类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
class Consumer extends Thread {
private Conn conn;
private int consumerNumber;
public Consumer(Conn conn1, int conNum) {
conn = conn1;
consumerNumber = conNum;
}
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("Consumer thread " + consumerNumber + " retrieved integer: " + conn.read()); //
try {
sleep((int) (Math.random() * 2000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
二. 生产者类
/**
* 生产者类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
class Producer extends Thread {
private Conn conn;
public Producer(Conn conn1) {
conn = conn1;
}
public void run() {
for (int i = 0; i < 20; i++) {
conn.add(i);
}
}
}
* 生产者类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
class Producer extends Thread {
private Conn conn;
public Producer(Conn conn1) {
conn = conn1;
}
public void run() {
for (int i = 0; i < 20; i++) {
conn.add(i);
}
}
}
三. 线程通信类
/**
* 线程通信类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
class Conn {
private int buffer[] = new int[200]; //10个Procuder线程,需存放200个变量
private int next = 0; //Flags to keep track of our int buffer status
private boolean isFull = false;
private boolean isEmpty = true;
/**
* method to read int
* @return
*/
public synchronized int read() {
while (isEmpty == true) { //We can't read if there is nothing in our int buffer
try {
wait();//we'll exit this when isEmpty turns false
}catch (InterruptedException e) {
e.printStackTrace();
}
}
next--; //decrement the count,since we're going to read one int
if (next == 0) {
isEmpty = true; //Did we read the last letter?
}
isFull = false;
notify();
return (buffer[next]);//return the int to the thread that is reading
}
/**
* method to add integer to the buffer
* @param number
*/
public synchronized void add(int number) {
while (isFull == true ) { //Wait around until there's room to add another letter
try {
wait();//This will exit when isFull turns false
}catch (InterruptedException e) {
e.printStackTrace();
}
}
next++; //add the integer to the next available spot buffer[next]=number;Change the next available spot
if (next == 200) {
isFull = true; //Are we full?
} else {
buffer[next] = number;
}
isEmpty =false;
notify();
}
}
* 线程通信类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
class Conn {
private int buffer[] = new int[200]; //10个Procuder线程,需存放200个变量
private int next = 0; //Flags to keep track of our int buffer status
private boolean isFull = false;
private boolean isEmpty = true;
/**
* method to read int
* @return
*/
public synchronized int read() {
while (isEmpty == true) { //We can't read if there is nothing in our int buffer
try {
wait();//we'll exit this when isEmpty turns false
}catch (InterruptedException e) {
e.printStackTrace();
}
}
next--; //decrement the count,since we're going to read one int
if (next == 0) {
isEmpty = true; //Did we read the last letter?
}
isFull = false;
notify();
return (buffer[next]);//return the int to the thread that is reading
}
/**
* method to add integer to the buffer
* @param number
*/
public synchronized void add(int number) {
while (isFull == true ) { //Wait around until there's room to add another letter
try {
wait();//This will exit when isFull turns false
}catch (InterruptedException e) {
e.printStackTrace();
}
}
next++; //add the integer to the next available spot buffer[next]=number;Change the next available spot
if (next == 200) {
isFull = true; //Are we full?
} else {
buffer[next] = number;
}
isEmpty =false;
notify();
}
}
四. 测试类
/**
* 测试类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
public class ProducerAndConsumerTest {
/**
* @param args
*/
public static void main(String[] args) {
Conn conn = new Conn();
Producer pro1 = new Producer(conn);
Producer pro2 = new Producer(conn);
Producer pro3 = new Producer(conn);
Producer pro4 = new Producer(conn);
Producer pro5 = new Producer(conn);
Producer pro6 = new Producer(conn);
Producer pro7 = new Producer(conn);
Producer pro8 = new Producer(conn);
Producer pro9 = new Producer(conn);
Producer pro10 = new Producer(conn);
Consumer consumer1 = new Consumer(conn, 1);
Consumer consumer2 = new Consumer(conn, 2);
Consumer consumer3 = new Consumer(conn, 3);
Consumer consumer4 = new Consumer(conn, 4);
pro1.start();
pro2.start();
pro3.start();
pro4.start();
pro5.start();
pro6.start();
pro7.start();
pro8.start();
pro9.start();
pro10.start();
consumer1.start();
consumer2.start();
consumer3.start();
consumer4.start();
}
}
* 测试类
* @author Amigo Xie(xiexingxing1121@126.com)
*
*/
public class ProducerAndConsumerTest {
/**
* @param args
*/
public static void main(String[] args) {
Conn conn = new Conn();
Producer pro1 = new Producer(conn);
Producer pro2 = new Producer(conn);
Producer pro3 = new Producer(conn);
Producer pro4 = new Producer(conn);
Producer pro5 = new Producer(conn);
Producer pro6 = new Producer(conn);
Producer pro7 = new Producer(conn);
Producer pro8 = new Producer(conn);
Producer pro9 = new Producer(conn);
Producer pro10 = new Producer(conn);
Consumer consumer1 = new Consumer(conn, 1);
Consumer consumer2 = new Consumer(conn, 2);
Consumer consumer3 = new Consumer(conn, 3);
Consumer consumer4 = new Consumer(conn, 4);
pro1.start();
pro2.start();
pro3.start();
pro4.start();
pro5.start();
pro6.start();
pro7.start();
pro8.start();
pro9.start();
pro10.start();
consumer1.start();
consumer2.start();
consumer3.start();
consumer4.start();
}
}
相关推荐
这是本人学习labview生产者消费者模型时,收藏的资料,里面包含生产消费者模型的文档介绍和例程,保证资源真实有效,例程能跑。
设计目的:通过研究Linux 的进程机制和信号量实现生产者消费者问题的并发控制。说明:有界缓冲区内设有20 个存储单元,放入/取出的数据项设定为1‐20 这20 个整型数。设计要求:1)每个生产者和消费者对有界缓冲区...
使用wait()和notify()实现的生产者与消费者模型,可以了解如何使用wait()和notify()进行线程间通信。(上一次上传的代码有一个问题没有考虑到,这次修补了——CSDN没法撤销资源,只能再上传了)
结合System V信号量机制,利用Linux下的多线程库实现了Linux下的操作系统生产者-消费者模型,具体原理可参考博文:: http://blog.csdn.net/Mikeoperfect/article/details/79431642
操作下系统的实验报告,第4个实验选的是那个设计一个消费者 生产者
(1)掌握基本的同步互斥算法,理解生产者和消费者同步的问题模型。 (2)了解Windows 2000/XP中多线程的并发执行机制,线程间的同步和互斥。 (3)学习使用Windows2000/XP中基本的同步对象,掌握相应的API。 2、...
这是一个maven+springmvc+dubbo+zookeeper的模型包括生产者、消费者、接口等。其实现可参考https://blog.csdn.net/mijichui2153/article/details/81102277。
例子中,生产者负责将1到1000的整数写入缓冲区,而消费者负责从同一个缓冲区中读取写入的整数并打印出来。因为生产者和消费者是两个同时运行的线程,并且要使用同一个缓冲区进行数据交换,因此必须利用一种机制进行...
在工作中,理解并运用生产者消费者模型能在高并发开发中出现更少的问题,本Demo最从简而行,使用Java语言实现一个最简单的生产者消费者模型。博客地址:https://blog.csdn.net/u012552275/article/details/104730175
EtherNet/IP(ethernet/Industrial Protocol)是由洛克威尔自动化公司开发的工业以太网通讯协定,由开放... 采用生产者/消费者模型实时交换控制数据。Ethernet/IP使用标准的IEEE802.3技术.采用TCP/IP技术传输CIP报文
我们采用了Go的携程+生产者消费者模型,让多个生产者发出消息,并同时让多个消费者监听返回,如果收到了对应的返回,则说明端口开放。这样的模型一方面可以实现并行从而加快扫描速度,另一方面使用异步模型,可以...
1.可以基于消费者生产者模型,采集频率和存储频率自由设定,不会丢失数据。 2.可选择存储数据,存储格式可选excel和txt,存储数据超最大限值报警。 3.温度湿度等参数可以实现真实数据采集(搭载MODBUS硬件,485串口...
使用生产者(排序线程)-消费者(Swing的GUI线程)模型实现归并排序的可视化。 这里可以看到运行效果图: http://blog.csdn.net/kakashi8841/article/details/7318618
一、课程设计目的 本课程设计是在嵌入式系统课程的基础上,通过软件编程及仿真调试的实践,进一步掌握嵌入式系统的原理和应用方法,是毕业设计前的一次重要实践,为...基于C语言的生产者-消费者模型设计 2.2 设计要求
具体博文详见: https://blog.csdn.net/yao_yaoya/article/details/128289611 主要介绍java的多线程所有内容: 1. 线程的三种创建方法;...3. 生产者与消费者模型的解决 等等...... 注:本人使用的是IDEA!
本文在内生增长的框架内,从减污技术出发分析倒U型环境库兹涅茨曲线形成的原因。...减污技术进步越快,人力资本开发效率越高,消费者更注重未来效用的获取,则稳态经济增长率便越高,同时环境质量改善速度也越快。
│ 高并发编程第一阶段26讲、多线程下的生产者消费者模型,以及详细介绍notifyAll方法.mp4 │ 高并发编程第一阶段27讲、wait和sleep的本质区别是什么,深入分析(面试常见问题).mp4 │ 高并发编程第一阶段28讲、...
第30节通过生产者消费者模型理解等待唤醒机制00:20:50分钟 | 第31节Condition的使用及原理解析00:17:40分钟 | 第32节使用Condition重写wait/notify案例并实现一个有界队列00:22:05分钟 | 第33节深入解析Condition...
对实际问题建模的时候,总会遇到一群或多群相联系的对象,比如工厂、消费者群体、交通工具和雇工等等。LINGO允许把这些相联系的对象聚合成集(sets)。一旦把对象聚合成集,就可以利用集来最大限度的发挥LINGO建模...
12.synchronized (生产者和消费) 13.String 和 StringBuffer 14.Serializable 15.MVC (Struts的工作流程) 16.什么是MDA 17.tcp与udp的区别 18.链表与散列表和数组的区别 19.堆和栈的区别 20.ejb的分类及...