1. Blocking queues and common Queues:
A queue is a basic data type. Its typical feature is FIFO.
The difference between a blocking queue and a common queue is:
When the queue is empty, the thread that obtains elements from the queue will be blocked until other threads Insert new elements into the empty queue;
When the queue is full, adding elements to the queue will be blocked until other threads remove one or more elements from the queue;
Medium: thread 1 adds elements to the blocking queue, while thread 2 removes elements from the blocking queue.
/*** Simple implementation of blocking queues */public class blockingqueue <t> {private list <t> queue = new queue list <t> (); Private int Limit = 10; public blockingqueue () {} public blockingqueue (INT limit) {This. limit = limit;} // enter the public synchronized void enqueue (t obj) throws interruptedexception {While (this. queue. size () = This. limit) {Wait ();} If (this. queue. size () = 0) {policyall ();} This. queue. add (OBJ);} // team-out public synchronized object dequeue () throws interruptedexception {While (this. queue. size () = 0) {Wait ();} If (this. queue. size () = This. limit) {policyall ();} return this. queue. remove (0 );}}
Ii. API implementation:
Blockingqueue is an interface with the following implementation classes:
1. arrayblockqueue: A Bounded blocking queue supported by arrays. This queue is sorted in FIFO order and its object size must be specified.
2. Define blockqueue: a blocking queue that can change the size. This queue is sorted according to the first-in-first-out principle. The size of the created object is not clear. In concurrent programs, the performance is slightly poor.
3. priorityblockingqueue: similar to linkedblockqueue. However, the sorting of objects contained in priorityblockingqueue is determined based on the natural sorting order of objects or the comparator of constructors.
4. synchronousqueue: synchronization queue. Each inserted queue must wait for another thread to be removed.
The following code uses a queue of three spaces to demonstrate the functions and effects of blocking queues.
Public class blockingqueuetest {public static void main (string [] ARGs) {// blocking queue class: three final blockingqueue <integer> queue = new arrayblockingqueue <integer> (3) can be stored in the queue. // enable three threads to continuously store data for (INT I = 0; I <3; I ++) {New thread () {public void run () {While (true) {try {thread. sleep (long) math. random () * 1000); system. out. println (thread. currentthread (). getname () + "prepare to put data! "); Queue. put (1); // store the data system in the queue. out. println (thread. currentthread (). getname () + "data has been put, and the queue currently has:" + queue. size () + "Data! "); Thread. sleep (1000);} catch (exception e) {e. printstacktrace ();}}}}. start () ;}// start a thread to continuously fetch data. New thread () {public void run () {While (true) {try {thread. sleep (1, 1000); system. out. println (thread. currentthread (). getname () + "prepare to retrieve data! "); Queue. take (); // retrieve data from the queue system. out. println (thread. currentthread (). getname () + "data has been removed. The queue currently has:" + queue. size () + "Data! ");} Catch (exception e) {e. printstacktrace () ;}}}. start ();}}
Thread-1: Prepare to put data! Thread-1 has data, and the queue currently has: 1 Data! Thread-0: Prepare to put data! Thread-0 has already put data, and the queue currently has: 2 Data! Thread-2 prepare to put data! Thread-2 has data, and the queue currently has three data! Thread-3 prepare to fetch data! Thread-3 has removed data. The queue currently has two data records! Thread-1: Prepare to put data! Thread-1 has data, and the queue currently has three data! Thread-0: Prepare to put data! Thread-2 prepare to put data! Thread-3 prepare to fetch data! Thread-3 has removed data. The queue currently has two data records! Thread-0 has data, and the queue currently has three data! Thread-1: Prepare to put data! Thread-0: Prepare to put data! Thread-3 prepare to fetch data! Thread-3 has removed data. The queue currently has two data records! Thread-2 has data, and the queue currently has three data!
New Features of jdk5: Blocking Queue (4)