[Java - Synchronization] Queues

1) Blocking Queue

A blocking queue is a queue that blocks when you attempt to:
  • dequeue from it and the queue is empty, or 
  • enqueue items to it and the queue is already full. 
Example use case: if you want some action to be executed only after 100 clients log-in.

1.1) Implementation

BlockingQueue<TYPE> queue = new LinkedTransferQueue<>();
BlockingQueue<TYPE> queue = new ArrayBlockingQueue<>();

1.2) Methods

put(..): Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full.

take(..): Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.

2) Delay Queue

An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired. 

3) Priority Queue

An unbounded blocking queue that orders the elements based on the generic's compareTo method. 
If generic is a custom class, then you will need to supply compareTo method.

Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS