Tuesday, 5 April 2016

What are the usage of Blocking queues ?


BlockingQueue
  • provides blocking put and take methods.
    • If the queue is full, put blocks until space becomes available.
    • If queue is empty, take blocks until an element is available.

Producer-Consumer design using BlockingQueue
  • Producers place data onto the queue as it becomes available
  • Consumer retrieves data from the queue when they are ready to take 
  • Example 
    • One person washes the dishes and place them in a rack - Producer
    • Other person take the dishes from the rack and dries them - Consumer
           ---- "consumes" wet dishes -> "produces" dry dishes
       
    • Here, dish rack acts as a BlockingQueue
      If there is no dish in the rack, consumer waits until dishes are available in rack

      If rack fills up, producer stops washing until there is more space.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.