Sunday, November 24, 2019

Producer Consumer Solution Using Blockingqueue Inward Coffee Thread

Producer Consumer job is 1 of the classic multi-threading problems inwards estimator scientific discipline together with the multi-threading world. It's tricky because it involves inter-thread communication, only it's of import because most of the multi-threading problems fits into this category. There are many ways to solve producer consumer job inwards Java e.g. you lot tin solve this past times using wait() together with notify() method, equally discussed here, or you lot tin job the Semaphore to solve this problem. In this article, you lot volition acquire a 3rd way to solve the producer-consumer job past times using the BlockingQueue inwards Java. It is arguably the simplest way to solve this job inwards whatever programming linguistic communication because blocking queue information construction non alone provides storage only also provides catamenia command together with thread-safety, which makes the code actually simple. Brian Goetz has also explained this cardinal degree together with designing inwards his classic Java Concurrency inwards Practice book, a must read for serious Java developers.



Producer Consumer Pattern using BlockingQueue

Java provides a built-in blocking queue information construction inwards java.util.concurrent package. It was added on JDK amongst multiple concurrent utilities e.g. CountDownLatch, CyclicBarrier, together with Callable together with Future classes.

The java.util.concurrent.BlockingQueue is an interface together with comes amongst 2 ready-made implementations thence ArrayLinkedBlockingQueue together with LinkedBlockingQueue. As the cite suggests, 1 is backed past times an array piece other is backed past times linked list.



In guild to solve the producer-consumer problem, nosotros volition create 2 threads which volition copy producer together with consumer together with instead of shared object nosotros volition job the shared BlockingQueue. Our code volition hold upwards simple, the producer will add together an chemical gene into queue together with consumer volition take the element.

BlockingQueue provides a put() method to shop the chemical gene together with take() method to cry upwards the element. Both are blocking method, which agency put() volition block if the queue has reached its capacity together with at that spot is no house to add together a novel element. Similarly, take() method volition block if blocking queue is empty. So, you lot tin run across that critical requirement of the producer-consumer designing is met correct there, you lot don't necessitate to position whatever thread synchronization code.

run() method, which adds 10 Integer object starting from 0.

After adding each element, the Producer thread is sleeping for 200 milliseconds past times calling Thread.sleep() method. This gives fourth dimension to the Consumer thread to swallow elements from Queue, that's why our code never blocks.

You tin run across that our Producer together with Consumer threads are working inwards sync because of Thread.sleep() nosotros receive got introduced afterward put() call. You tin farther experiment amongst the code past times removing the code to pause the Producer thread or inserting recess on Consumer thread to  create scenarios where Queue is total or empty.


Benefits of using BlockingQueue to solve Producer Consumer
  • Simple code, much to a greater extent than readable
  • less fault prone equally you lot don't receive got to bargain amongst whatever external synchronization

That's all virtually how to solve producer consumer job using BlockingQueue inwards Java. In production code, you lot should e'er job BlockingQueue, using wait() together with notify() is neither slowly non desirable given you lot receive got amend tools available. Even Joshua Bloch's has advised inwards Effective Java to prefer higher concurrency utilities together with libraries instead of writing your ain code. Remember, the code is alone written 1 time only read numerous fourth dimension for maintenance, troubleshooting together with back upwards purpose.

Further Learning
Multithreading together with Parallel Computing inwards Java
answer)
  • The divergence betwixt synchronized block together with methods inwards Java? (answer)
  • The divergence betwixt Callable together with Runnable inwards Java? (answer)
  • Difference betwixt extends Thread vs implements Runnable inwards Java? (answer)
  • When to job the volatile variable inwards Java? (answer)

  • No comments:

    Post a Comment