How it works...

In this recipe, you have used the LinkedTransferQueue class parameterized with the String class to implement the producer/consumer problem. This LinkedTransferQueue class is used as a buffer to share the data between producers and consumers.

You have implemented a Producer class that adds strings to the buffer using the put() method. You have executed 100 producers and every producer inserts in the buffer 10,000 strings, so you insert 1,000,000 strings in the buffer. The put() method adds the element at the end of the buffer.

You also have implemented a Consumer class, which gets a string from the buffer using the take() method. This method returns and deletes the first element of the buffer. If the buffer is empty, the method ...

Get Java 9 Concurrency Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.