Put (s) and take (s) in BlockingQueue Java

Here is an example.

class Factory {
    Queue<Object> queue = new LinkedBlockingQueue<Object>();

    public Object consume() {
        queue.take();
    }

    public void produce() {
        for (int i = 0; i < 2; i++) {
            queue.put(new Object());
        }
    }
}

For example, I have two threads, which are both called consume (). They are waiting for the producer to put something in the queue. My question is, does the take () action occur after the put () action, or is it possible that two put () actions occur one after another and only then do the threads wait?

Thank.

+3
source share
1 answer
0

All Articles