我是靠谱客的博主 轻松楼房,这篇文章主要介绍Java核心多线程 -- 消费者生产者Demo一,现在分享给大家,希望可以做个参考。

目的

  实现1个生产者和多个消费者合作工作的模式

手段

  使用wait、notify和synchronized实现线程间同步;使用ConcurrentLinkedQueue同步队列

 

 

生成和消费的pojo类

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class AlarmBookPojo { private int iDeviceId; public int getiDeviceId() { return iDeviceId; } public void setiDeviceId(int iDeviceId) { this.iDeviceId = iDeviceId; } }

 

生产者和消费者同步的队列:

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class QueueMgr { // 同步队列 ConcurrentLinkedQueue<AlarmBookPojo> queue = new ConcurrentLinkedQueue<AlarmBookPojo>(); private final static Byte[] synAlarm = new Byte[0]; //同步对象 /** * 返回值有可能为null * @return */ public AlarmBookPojo get(){ AlarmBookPojo o = null; o = queue.poll(); if(o == null){ synchronized(synAlarm){ try { synAlarm.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return o; } public boolean put(AlarmBookPojo o){ boolean bRtn = this.queue.offer(o); synchronized(synAlarm){ synAlarm.notify(); } return bRtn; } }

 

 

生产者:

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Productor implements Runnable { private QueueMgr queue; private int i = 0; public Productor(QueueMgr queue){ this.queue = queue; } public void generate(String sThreadName){ AlarmBookPojo o = new AlarmBookPojo(); o.setiDeviceId(i ++ ); queue.put(o); System.out.println(sThreadName + "put pojo."); } public void run(){ while(true){ this.generate(Thread.currentThread().getName()); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
 

 

消费者

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class Consumer implements Runnable{ private QueueMgr queue; public Consumer(QueueMgr queue){ this.queue = queue; } public void consumer(String sThreadName){ AlarmBookPojo o = queue.get(); if(o != null){ System.out.println(sThreadName + " Comsumer. o = " + o.getiDeviceId()); } } public void run(){ while(true){ try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.consumer(Thread.currentThread().getName()); } } }
 

 

main函数:

 

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
public class Main { public static void main(String[] args){ QueueMgr q = new QueueMgr(); new Thread(new Consumer(q)).start(); new Thread(new Productor(q)).start(); // new Thread(new Productor(q)).start(); // new Thread(new Productor(q)).start(); new Thread(new Consumer(q)).start(); new Thread(new Consumer(q)).start(); } }
 

 

 

执行以上程序,正常工作

 

 

Thread-1put pojo.

Thread-1put pojo.

Thread-0 Comsumer. o = 0

Thread-2 Comsumer. o = 1

Thread-1put pojo.

Thread-0 Comsumer. o = 2

Thread-1put pojo.

Thread-3 Comsumer. o = 3

 

 

参考文献

 

  • 多线程基础总结一--synchronized(1)  synchronized的使用方法和注意实现;Monitor Object模式
  • 多线程基础总结六--synchronized(2)  synchronized的重要使用对象的隐式锁
  • 多线程基础总结十一--ConcurrentLinkedQueue  

 

 

 

 

 

最后

以上就是轻松楼房最近收集整理的关于Java核心多线程 -- 消费者生产者Demo一的全部内容,更多相关Java核心多线程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部