Java 5 multithreading (8)-arrayblockingqueue blocking queue

Source: Internet
Author: User
The difference between a blocking queue and a non-blocking queue: If the queue is full, the thread will always be blocked if the queue is blocked, and an exception will be thrown if the queue is not blocked. A queue also includes a queue with a fixed length and a queue with an unfixed length. this class implements the blockingqueue interface using the following methods:


In the case of insert, if the queue is full, an exception will be thrown when the add method is used to put it in the queue. If the offer method is used to put it back to false, the put () method will block it, know that you have space to store. the following is an example: public class blockingqueuetest {

Public static void main (string [] ARGs) {final blockingqueue <integer> queue = new arrayblockingqueue <integer> (3 ); // This queue can contain only three integers for (INT I = 0; I <2; 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); // system will be blocked if it is full. out. println (thread. currentthread (). getname () + "already put data," + "queue currently has" + queue. size () + "data");} catch (interruptedexception e) {e. printstacktrace ();}}}}. start () ;}new thread () {public void run () {While (true) {try {// change the sleep time here to 100 and 1000, respectively, observe the running result thread. sleep (1, 1000); system. out. println (thread. currentthread (). getname () + "prepare to retrieve data! "); Queue. take (); // if no data exists, the system will be blocked. out. println (thread. currentthread (). getname () + "data has been removed," + "queue currently has" + queue. size () + "data");} catch (interruptedexception e) {e. printstacktrace ();}}}}. start ();}

}

Running result:


We can implement the synchronous notification function by using the Buffer Queue of both controls. Static class business {

Blockingqueue <integer> queue1 = new arrayblockingqueue <integer> (1); blockingqueue <integer> queue2 = new arrayblockingqueue <integer> (1); {// sets the anonymous constructor, it will be executed before the object is instantiated. try {// because the main thread needs to be executed first. queue2.put (1);} catch (interruptedexception e) {e. printstacktrace () ;}} public void sub (int K) {// synchronized cannot be added, which may lead to a deadlock. The put method will block but will not release the lock try {queue2.put (1 ); // block} catch (interruptedexception e) {e. printstacktrace () ;}for (INT I = 1; I <= 10; I ++) {system. out. println ("sub thread sequence" + I + "loop of" + k);} Try {queue1.take (); // then queue1 can be put} catch (interruptedexception E) {e. printstacktrace () ;}} public void main (int K) {// synchronized cannot be added, which may lead to a deadlock. The put method will block but will not release the lock try {queue1.put (1 ); // you can put} catch (interruptedexception e) {e. printstacktrace () ;}for (INT I = 1; I <= 100; I ++) {system. out. println ("main thread sequence" + I + "loop of" + k);} Try {queue2.take (); // then queue2 can be put} catch (interruptedexception E) {e. printstacktrace ();}}

}

This can also achieve the main thread and sub-thread between the printing switch. reprint please indicate the source: http://blog.csdn.net/johnny901114/article/details/8696026

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.