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, we implemented an application that uses ConcurrentHashMap to store information about operations made by users. Internally, the hash table uses the user attribute of the Operation class as a key and ConcurrentLinkedDeque (a non-blocking concurrent list) as its value to store all the operations associated with that user.

First, we filled the hash with some random data using 10 different threads. We implemented the HashFiller task for this purpose. The biggest problem with these tasks is what happens when you have to insert a key in the hash table. If two threads want to add the same key at the same time, you can lose the data inserted by one of the threads and have a data-race condition. To solve this problem, ...

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