O'Reilly logo

Java 9 Concurrency Cookbook - Second Edition by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required