java block queue BlockingQueue detail and example

  • 2020-06-19 10:09:31
  • OfStack

Block queue BlockingQueue details and examples

BlockingQueue data transmission, good to solve the multiple threads first BlockingQueue is an interface, it roughly four implementation class, this is a very special queue, if BlockQueue is empty, from BlockingQueue operation will be block has reached the awaited state, until BlockingQueue into the things will be awakened. Similarly, if BlockingQueue is full, any attempt to save what operation will also be blocking has reached the awaited state, until BlockingQueue in space will be awakened to continue operating.

The four implementation classes of BlockingQueue:

ArrayBlockingQueue: A specified size of BlockingQueue whose constructor must take one int argument to indicate its size. The objects it contains are sorted in FIFO(first in, first out) order.

2.LinkedBlockingQueue: For a variable size BlockingQueue, if its constructor takes a specified size parameter, the generated BlockingQueue has a size limit. Without the size parameter, the generated BlockingQueue is determined by Integer.MAX_ES30en. The objects it contains are sorted in FIFO(first in, first out) order

3.PriorityBlockingQueue: Similar to LinkedBlockQueue, but the sort of objects it contains is not FIFO, but depends on the natural sort order of the objects or the order determined by the constructor Comparator.

4.SynchronousQueue: Special BlockingQueue, to which operation must be done alternately put and take.

Common methods of BlockingQueue:

1)add(anObject): Add anObject to BlockingQueue, that is, if BlockingQueue can be contained, return true; otherwise, exception will be reported

2)offer(anObject): if possible, add anObject to BlockingQueue, that is, return true if BlockingQueue can accommodate, otherwise return false.

3)put(anObject): Add anObject to BlockingQueue. If BlockQueue has no space, the thread calling this method is blocked until BlockingQueue has room to continue.

4)poll(time): Take the first object in BlockingQueue. If it cannot be taken out immediately, it can wait for the time specified by the parameter time and return null if it cannot be taken

5)take(): The first object in BlockingQueue is taken. If BlockingQueue is empty, the object is blocked and enters the waiting state until a new object in Blocking is added

Example:

This example mainly simulates the workflow between producers and consumers. It is a simple scenario in which consumers wait for producers to produce products for consumers to consume.

Producers:


package com.gefufeng;

import java.util.concurrent.BlockingQueue;

public class Producter implements Runnable{
 private BlockingQueue<String> blockingQueue;
 
 public Producter(BlockingQueue<String> blockingQueue){
 this.blockingQueue = blockingQueue;
 }

 @Override
 public void run() {
 try {
  blockingQueue.put(" I produced " + Thread.currentThread().getName());
  System.out.println(" I produced " + Thread.currentThread().getName());
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  System.out.println(" Production of failure ");
 }
 
 }
 
 

}

Consumer:


package com.gefufeng;

import java.util.concurrent.BlockingQueue;

public class Customer implements Runnable{
 private BlockingQueue<String> blockingQueue;
 
 public Customer(BlockingQueue<String> blockingQueue){
 this.blockingQueue = blockingQueue;
 }

 @Override
 public void run() {
 for(;;){
  try {
  String threadName = blockingQueue.take();
  System.out.println(" Remove: " + threadName);
  } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  System.out.println(" Take out the failure ");
  }
 }
 }

}

Implementation class:


package com.gefufeng;

import java.util.concurrent.ArrayBlockingQueue;

public class Executer {
 
 public static void main(String[] args) {
 ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(2);
 Producter producter = new Producter(arrayBlockingQueue);
 Customer cusotmer = new Customer(arrayBlockingQueue);
 new Thread(cusotmer).start();
 for(;;){
  try {
  Thread.sleep(2000);
  new Thread(producter).start();
  } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }
 }
 
 
 
 }

}

First is waiting for the product, the consumer cycle. When the first loop execution blockingQueue take (), which is to come up with any of the products, then enter the blocking state, two seconds later, the producer produces one product, then blockingQueue get products, print the log, then consumers second execution cycle, found blockingQueue. take () didn't get the product again, and again into the blocked state... In turn, cycle

Thank you for reading, I hope to help you, thank you for your support!


Related articles: