BlockingQueue
2016-07-28 13:11
471 查看
The Java
this
This text will not discuss how to implement a
Queues in my more theoretical Java Concurrency Tutorial.
A
The producing thread will keep producing new objects and insert them into the queue, until the queue reaches some upper bound on what it can contain. It's limit, in other words. If the blocking queue reaches its upper limit, the producing thread is blocked
while trying to insert the new object. It remains blocked until a consuming thread takes an object out of the queue.
The consuming thread keeps taking objects out of the blocking queue, and processes them. If the consuming thread tries to take an object out of an empty queue, the consuming thread is blocked until a producing thread puts an object into the queue.
is a table of the methods:
The 4 different sets of behaviour means this:
Throws Exception:
If the attempted operation is not possible immediately, an exception is thrown.
Special Value:
If the attempted operation is not possible immediately, a special value is returned (often true / false).
Blocks:
If the attempted operation is not possible immedidately, the method call blocks until it is.
Times Out:
If the attempted operation is not possible immedidately, the method call blocks until it is, but waits no longer than the given timeout. Returns a special value telling whether the operation succeeded or not (typically true / false).
It is not possible to insert
It is also possible to access all the elements inside a
You can then call e.g.
have to.
Since
(in Java 6):
ArrayBlockingQueue
DelayQueue
LinkedBlockingQueue
PriorityBlockingQueue
SynchronousQueue
Click the links in the list to read more about each implementation. If a link cannot be clicked, that implementation has not yet been described. Check back again in the future, or check out the JavaDoc's for more detail.
Here is a Java
First, the
strings into a shared
Here is the
the queue.
Here is the
BlockingQueueinterface in the
java.util.concurrentpackage represents a queue which is thread safe to put into, and take instances from. In this text I will show you how to use
this
BlockingQueue.
This text will not discuss how to implement a
BlockingQueuein Java yourself. If you are interested in that, I have a text on Blocking
Queues in my more theoretical Java Concurrency Tutorial.
BlockingQueue Usage
A BlockingQueueis typically used to have on thread produce objects, which another thread consumes. Here is a diagram that illustrates this principle:
![]() |
A BlockingQueue with one thread putting into it, and another thread taking from it. |
while trying to insert the new object. It remains blocked until a consuming thread takes an object out of the queue.
The consuming thread keeps taking objects out of the blocking queue, and processes them. If the consuming thread tries to take an object out of an empty queue, the consuming thread is blocked until a producing thread puts an object into the queue.
BlockingQueue Methods
ABlockingQueuehas 4 different sets of methods for inserting, removing and examining the elements in the queue. Each set of methods behaves differently in case the requested operation cannot be carried out immediately. Here
is a table of the methods:
Throws Exception | Special Value | Blocks | Times Out | |
Insert | add(o) | offer(o) | put(o) | offer(o, timeout, timeunit) |
Remove | remove(o) | poll() | take() | poll(timeout, timeunit) |
Examine | element() | peek() |
Throws Exception:
If the attempted operation is not possible immediately, an exception is thrown.
Special Value:
If the attempted operation is not possible immediately, a special value is returned (often true / false).
Blocks:
If the attempted operation is not possible immedidately, the method call blocks until it is.
Times Out:
If the attempted operation is not possible immedidately, the method call blocks until it is, but waits no longer than the given timeout. Returns a special value telling whether the operation succeeded or not (typically true / false).
It is not possible to insert
nullinto a
BlockingQueue. If you try to insert null, the
BlockingQueuewill throw a
NullPointerException.
It is also possible to access all the elements inside a
BlockingQueue, and not just the elements at the start and end. For instance, say you have queued an object for processing, but your application decides to cancel it.
You can then call e.g.
remove(o)to remove a specific object in the queue. However, this is not done very efficiently, so you should not use these
Collectionmethods unless you really
have to.
BlockingQueue Implementations
Since BlockingQueueis an interface, you need to use one of its implementations to use it. The
java.util.concurrentpackage has the following implementations of the
BlockingQueueinterface
(in Java 6):
ArrayBlockingQueue
DelayQueue
LinkedBlockingQueue
PriorityBlockingQueue
SynchronousQueue
Click the links in the list to read more about each implementation. If a link cannot be clicked, that implementation has not yet been described. Check back again in the future, or check out the JavaDoc's for more detail.
Java BlockingQueue Example
Here is a Java BlockingQueueexample. The example uses the
ArrayBlockingQueueimplementation of the
BlockingQueueinterface.
First, the
BlockingQueueExampleclass which starts a
Producerand a
Consumerin separate threads. The
Producerinserts
strings into a shared
BlockingQueue, and the
Consumertakes them out.
public class BlockingQueueExample { public static void main(String[] args) throws Exception { BlockingQueue queue = new ArrayBlockingQueue(1024); Producer producer = new Producer(queue); Consumer consumer = new Consumer(queue); new Thread(producer).start(); new Thread(consumer).start(); Thread.sleep(4000); } }
Here is the
Producerclass. Notice how it sleeps a second between each
put()call. This will cause the
Consumerto block, while waiting for objects in
the queue.
public class Producer implements Runnable{ protected BlockingQueue queue = null; public Producer(BlockingQueue queue) { this.queue = queue; } public void run() { try { queue.put("1"); Thread.sleep(1000); queue.put("2"); Thread.sleep(1000); queue.put("3"); } catch (InterruptedException e) { e.printStackTrace(); } } }
Here is the
Consumerclass. It just takes out the objects from the queue, and prints them to
System.out.
public class Consumer implements Runnable{ protected BlockingQueue queue = null; public Consumer(BlockingQueue queue) { this.queue = queue; } public void run() { try { System.out.println(queue.take()); System.out.println(queue.take()); System.out.println(queue.take()); } catch (InterruptedException e) { e.printStackTrace(); } } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序