Java-17.5 blocking Queue (taking producer consumer mode as an example)

Source: Internet
Author: User

Java-17.5 blocking Queue (taking producer consumer mode as an example)

In this section, we will discuss blocking queues. The following describes the blocking queue through the producer consumer mode.

1. What is a blocking queue? (Translated from http://tutorials.jenkov.com/java-concurrency/blocking-queues.html)

When the queue is empty, operations to obtain elements from the queue will be blocked, or when the queue is full, operations to add elements to the queue will be blocked. The thread that tries to obtain elements from an empty blocking queue will be blocked until other threads Insert new elements into the empty queue. Similarly, threads trying to add new elements to a full blocking queue will also be blocked until other threads make the queue idle again, for example, removing one or more elements from the queue or clearing the queue completely shows how to cooperate by blocking the queue:

 

2. Features

(1) first-in-first-out

(2) thread synchronization

 

3. The following describes the use of blocking queues in combination with the producer and consumer modes.

 

Package com. ray. ch17; import java. util. concurrent. define blockingqueue; public class Test {public static void main (String [] args) {Basket basket = new Basket (); Thread thread1 = new Thread (new Chief (basket )); thread thread2 = new Thread (new Customer (basket); Thread thread3 = new Thread (new Customer (basket); thread1.start (); thread2.start (); thread3.start ();}} class Cake {private static int index = 0; private final int id = index ++; public int getId () {return id;} public Cake () {System. out. println ("generated cake, id:" + id) ;}} class Chief implements Runnable {private Basket basket = null; public Chief (Basket basket) {this. basket = basket;} public void makeCake () throws InterruptedException {for (int I = 0; I <20; I ++) {basket. put (new Cake (); System. out. println ("Number of cakes in the current box:" + basket. getQueueSize (); Thread. sleep (20) ;}@ Overridepublic void run () {try {makeCake ();} catch (InterruptedException e) {e. printStackTrace () ;}} class Customer implements Runnable {private Basket basket = null; public Customer (Basket basket) {this. basket = basket;} public void buyCake () throws InterruptedException {System. out. println ("consumed cake, id:" + basket. take (). getId (); System. out. println ("Number of cakes in the current box:" + basket. getQueueSize () ;}@ Overridepublic void run () {try {for (int I = 0; I <10; I ++) {buyCake (); Thread. sleep (100) ;}} catch (InterruptedException e) {e. printStackTrace () ;}} class Basket {private javasblockingqueue
 
  
Queue = new LinkedBlockingQueue
  
   
(10); public void put (Cake cake) throws InterruptedException {queue. put (cake);} public Cake take () throws InterruptedException {Cake cake = queue. take (); return cake;} public int getQueueSize () {return queue. size ();}}
  
 
Output:

Produced cake, id: 0
Consumed cake, id: 0
Number of cakes in the box: 0
Number of cakes in the box: 0
Produced cake, id: 1
Number of cakes in the box: 1
Consumed cake, id: 1
Number of cakes in the box: 0
Produced cake, id: 2
Number of cakes in the box: 1
Produced cake, id: 3
Number of cakes in the box: 2
Produced cake, id: 4
Number of cakes in the box: 3
Consumed cake, id: 2
Number of cakes in the box: 2
Produced cake, id: 5
Number of cakes in the box: 3
Consumed cake, id: 3
Number of cakes in the box: 2
Produced cake, id: 6
Number of cakes in the box: 3
Produced cake, id: 7
Number of cakes in the box: 4
Produced cake, id: 8
Number of cakes in the box: 5
Produced cake, id: 9
Number of cakes in the box: 6
Consumed cake, id: 4
Number of cakes in the box: 5
Consumed cake, id: 5
Number of cakes in the box: 4
Consumed cake, id: 6
Number of cakes in the box: 3
Consumed cake, id: 7
Number of cakes in the box: 2
Consumed cake, id: 8
Number of cakes in the box: 1
Consumed cake, id: 9
Number of cakes in the box: 0

Explanation:

(1) because it is a producer consumer model, there must be four categories of producers, consumers, baskets, and consumed items (cakes), and a Test class.

(2) In the Cake class, we use the static index to record the id of each Cake.

(3) In the Basket class, in addition to the put and take methods, we also put a getSize method to get the number of Cake in the Basket.

(4) There is actually only one way for producers and consumers to produce make or buy. Note that there are two places. The first one is the number, because we used a blocking queue, therefore, when the total production quantity is greater than the total consumption quantity, the program will remain in wait and will continue to be executed only when the next consumer consumes the product. Otherwise, this can also happen if the consumption quantity is greater than the production quantity. The second is the time. We need to match the time according to the actual time matching relationship. If the production is larger than the consumption, when one consumer is produced, the producer needs to be added. If the consumption is greater than the production, it is determined based on actual needs.

The following code needs to wait because the total consumption is greater than the total production quantity:

Package com. ray. ch17; import java. util. concurrent. define blockingqueue; public class Test {public static void main (String [] args) {Basket basket = new Basket (); Thread thread1 = new Thread (new Chief (basket )); thread thread2 = new Thread (new Customer (basket); Thread thread3 = new Thread (new Customer (basket); thread1.start (); thread2.start (); thread3.start ();}} class Cake {private static int index = 0; private final int id = index ++; public int getId () {return id;} public Cake () {System. out. println ("generated cake, id:" + id) ;}} class Chief implements Runnable {private Basket basket = null; public Chief (Basket basket) {this. basket = basket;} public void makeCake () throws InterruptedException {for (int I = 0; I <20; I ++) {basket. put (new Cake (); System. out. println ("Number of cakes in the current box:" + basket. getQueueSize (); Thread. sleep (200) ;}@ Overridepublic void run () {try {makeCake ();} catch (InterruptedException e) {e. printStackTrace () ;}} class Customer implements Runnable {private Basket basket = null; public Customer (Basket basket) {this. basket = basket;} public void buyCake () throws InterruptedException {System. out. println ("consumed cake, id:" + basket. take (). getId (); System. out. println ("Number of cakes in the current box:" + basket. getQueueSize () ;}@ Overridepublic void run () {try {for (int I = 0; I <20; I ++) {buyCake (); Thread. sleep (100) ;}} catch (InterruptedException e) {e. printStackTrace () ;}} class Basket {private javasblockingqueue
 
  
Queue = new LinkedBlockingQueue
  
   
(10); public void put (Cake cake) throws InterruptedException {queue. put (cake);} public Cake take () throws InterruptedException {Cake cake = queue. take (); return cake;} public int getQueueSize () {return queue. size ();}}
  
 
Summary: This chapter introduces the features of blocking queues through the producer consumer model.

This chapter is here. Thank you.

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.