博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java SynchronousQueue
阅读量:6705 次
发布时间:2019-06-25

本文共 3633 字,大约阅读时间需要 12 分钟。

This Java tutorial is to learn about the concurrent collection SynchronousQueue. It is an implementation of BlockingQueue. Among all Java concurrent collections, SynchronousQueue is different. Capacity of a synchrounous queue is always zero. It is because in SynchronousQueue an insert will wait for a remove operation by another thread and vice versa.

  • put() call to a SynchronousQueue will not return until there is a corresponding take() call.
  • peek is not possible with a SynchronousQueue
  • As there is no element iteration is also not possible.
  • Insert is not possible if there is a thread trying to remove it.
  • SynchronousQueue should be imagined like a baton in a relay race.
  • If there are more than one thread waiting for a removal so that they can do insert then with fairness set to true, threads are granted access in FIFO order.
  • SynchronousQueue is the default BlockingQueue used for the Executors.newCachedThreadPool() methods.

SynchronousQueue Example

Let us have a look at a producer-consumer based example to understand the SynchronousQueue.

SynchronousQueueProducer.java

package com.javapapers.java.collections;import java.util.Random;import java.util.UUID;import java.util.concurrent.BlockingQueue;public class SynchronousQueueProducer implements Runnable {	protected BlockingQueue
blockingQueue; final Random random = new Random(); public SynchronousQueueProducer(BlockingQueue
queue) { this.blockingQueue = queue; } @Override public void run() { while (true) { try { String data = UUID.randomUUID().toString(); System.out.println("Put: " + data); blockingQueue.put(data); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }}

  

SynchronousQueueConsumer.java

package com.javapapers.java.collections;import java.util.concurrent.BlockingQueue;public class SynchronousQueueConsumer implements Runnable {	protected BlockingQueue
blockingQueue; public SynchronousQueueConsumer(BlockingQueue
queue) { this.blockingQueue = queue; } @Override public void run() { while (true) { try { String data = blockingQueue.take(); System.out.println(Thread.currentThread().getName() + " take(): " + data); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }}

  

SynchronousQueueExample.java

package com.javapapers.java.collections;import java.util.concurrent.BlockingQueue;import java.util.concurrent.SynchronousQueue;public class SynchronousQueueExample {	public static void main(String[] args) {		final BlockingQueue
synchronousQueue = new SynchronousQueue
(); SynchronousQueueProducer queueProducer = new SynchronousQueueProducer( synchronousQueue); new Thread(queueProducer).start(); SynchronousQueueConsumer queueConsumer1 = new SynchronousQueueConsumer( synchronousQueue); new Thread(queueConsumer1).start(); SynchronousQueueConsumer queueConsumer2 = new SynchronousQueueConsumer( synchronousQueue); new Thread(queueConsumer2).start(); }}

  

Example Output

Put: 4c34138b-51dd-4c2c-9633-87a147253978Thread-2 take(): 4c34138b-51dd-4c2c-9633-87a147253978Put: 39682e81-9ba8-4b29-a37e-3dc4d5cdb1f7Thread-1 take(): 39682e81-9ba8-4b29-a37e-3dc4d5cdb1f7Put: e21dbfd2-d007-43d2-9733-cb10ce683d81Thread-2 take(): e21dbfd2-d007-43d2-9733-cb10ce683d81Put: 556c5584-7154-4117-a0f4-f52ec8dcfad8Thread-1 take(): 556c5584-7154-4117-a0f4-f52ec8dcfad8Put: 1da7f16c-c030-44ed-be02-8983b7387497Thread-2 take(): 1da7f16c-c030-44ed-be02-8983b7387497

  

原文连接:

翻译:

 

转载于:https://www.cnblogs.com/chenjunjie12321/p/9516068.html

你可能感兴趣的文章
IE中input标签密码框与文本框宽度不一样问题
查看>>
【系统架构师修炼之道】(10):绪论——系统架构师的定义与职业素质
查看>>
Uber 开源地理可视化工具 Ketoper.gl,加速数据处理
查看>>
NSDate格式化小例
查看>>
运维不容错过的4个关键指标!
查看>>
spring 基础
查看>>
商品详情页上拉查看详情
查看>>
Kubernetes DNS服务简介
查看>>
windbg调试堆破坏
查看>>
How to Install CMS Made Simple v2.2 on LAMP in CentOS 7.2
查看>>
新IT铺路 智慧出行时代来了!
查看>>
虚拟机上keepalived实验笔记
查看>>
ElasticSearch(java) 创建索引
查看>>
手把手教你在多种无监督聚类算法实现Python(附代码)
查看>>
第4章 Keras入门
查看>>
手工修复ie浏览器
查看>>
BATJ互掐,哪家AI公司首先达到万亿美元市值? | 新智元AI技术峰会论坛
查看>>
hdu 1232 畅通工程 (并查集)
查看>>
MySql的用户权限
查看>>
java中finally和return的执行顺序
查看>>