Friday, December 27, 2019

LinkedBlockingQueue

import java.util.concurrent.LinkedBlockingQueue;

public class MyLinkedBlockingQueue {
    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingQueue  linkedBlockingQueue = new LinkedBlockingQueue();
        linkedBlockingQueue.put("PIPPO");
        linkedBlockingQueue.put("PLUTO");
        System.out.println(linkedBlockingQueue);
        Object bla1 = linkedBlockingQueue.peek();
        System.out.println(bla1);
        Object bla2 = linkedBlockingQueue.take();
        System.out.println(bla2);
        Object bla3 = linkedBlockingQueue.take();
        System.out.println(bla3);
        Object bla4 = linkedBlockingQueue.take();
        System.out.println(bla4);

        System.out.println(linkedBlockingQueue);
    }
}



the output is:

[PIPPO, PLUTO]
PIPPO // this is the peek
PIPPO // this is the first take
PLUTO // this is the second take

and then at bla4 it blocks, because the Queue is empty...waiting for someone to put something into it...

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/LinkedBlockingQueue.html




No comments: