当前位置:网站首页>BlockingQueue four sets of APIs

BlockingQueue four sets of APIs

2022-06-22 06:18:00 Kuxiaoya

Blocking queues

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mOzXsHGu-1654973647624)(C:\Users\38492\AppData\Local\Temp\1654970501452.png)]

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-xd30NitP-1654973647634)(C:\Users\38492\AppData\Local\Temp\1654970625596.png)]

Under what circumstances will we use Blocking queues ?

Multithreading concurrent processing , Thread pool !

Be sure to learn to use queues

add to remove

BlockingQueue( Blocking queues ) Four groups API

The way Throw an exception There is a return value , Don't throw exceptions Block waiting Overtime waiting
add to addofferputoffer
remove removepolltakepoll
First element of inspection team elementpeek--

The first group :( Throw an exception )

package bq;

import java.util.concurrent.ArrayBlockingQueue;

public class Test {
    
    public static void main(String[] args) {
    
        test1();
    }

    /** *  Throw an exception  */
    public static void test1(){
    
        // The size of the queue 
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.add("a"));
        System.out.println(blockingQueue.add("b"));
        System.out.println(blockingQueue.add("c"));

        // System.out.println(blockingQueue.element());// See who is the head of the team  
        
        // The queue is 3, We add a fourth element  : IllegalStateException  Throw an exception !( Illegal status exception )
        //System.out.println(blockingQueue.add("d"));
        System.out.println("----------------");
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());

        //NoSuchElementException  Throw an exception !( There is no such element exception )
        //System.out.println(blockingQueue.remove());
    }
}

The second group ( There is a return value , Don't throw exceptions )

package bq;

import java.util.concurrent.ArrayBlockingQueue;

public class Test {
    
    public static void main(String[] args) {
    
        test2();
    }

    /** *  There is a return value , Don't throw exceptions  */
    public static void test2(){
    
        // The size of the queue 
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        
        //System.out.println(blockingQueue.peek());// See who is the head of the team 
         
        //false  Don't throw exceptions !
        //System.out.println(blockingQueue.offer("d"));

        System.out.println("----------------");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //null  Don't throw exceptions !
        System.out.println(blockingQueue.poll());
    }
}

The third group ( Block waiting )

package bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Test {
    
    public static void main(String[] args) throws InterruptedException {
    
        test3();
    }

    /** *  wait for , Blocking ( Keep blocking ) */
    public static void test3() throws InterruptedException {
    
        // The size of the queue 
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        // Keep blocking 
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        //blockingQueue.put("d"); //  The queue has no place , Keep blocking 

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        //System.out.println(blockingQueue.take()); //  There is no such element , Keep blocking 
    }
}

Group 4 ( Overtime waiting )

package bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class Test {
    
    public static void main(String[] args) throws InterruptedException {
    
        test4();
    }

    /** *  wait for , Blocking ( Time out blocking ) */
    public static void test4() throws InterruptedException {
    
        // The size of the queue 
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        //System.out.println(blockingQueue.offer("d", 2,TimeUnit.SECONDS));//  Wait more than 2 Seconds to exit 

        System.out.println("--------------");
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        //System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));//  Wait more than 2 Seconds to exit 
    }
}
原网站

版权声明
本文为[Kuxiaoya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220609550536.html