In this example I demonstrate how to use ConcurrentMap along with Future to generate a lazy cache. Concurrent maps are a good choice in some situations, where absolute atomicity can be traded off for performance, some level of control over ordering of events has been traded for performance. Let’s assume for the sake of example that we have an object that is heavy to create. Further, it has to be created on a single thread because it calls into a single threaded maths library.
CountDownLatch provides a means of waiting for a number of asynchronous events before proceeding. In order to do this one constructs a latch providing the event count. Then one thread would normally call await Whilst the other thread calls countDown. Once the count reaches zero the await call returns and the latch is set. If the call to await happens after the latch is set it returns immediately. In our example we need to wait for a thread to initialise before proceeding.
In this example I show an usage of two more concurrent classes CyclicBarrier and Semaphore; which are both provided in the core Java library. There's a wealth of concurrency classes built directly into the JVM that can really simplify multi-threaded development. CyclicBarrrier to make threads wait for alignment. In the example below, I use a cyclic barrier to make several threads wait for alignment. This is a common example where we have many threads and need to wait for all threads to reach a barrier before proceeding:
In this BlockingQueue example we show how to write a very simple producer - consumer with a blocking queue. This example generates SimpleAddition objects that require an addition of two numbers to be performed on the consumer thread. In this case the two values to be added are generated using java.util.Random's nextInt call. They are stored on the queue as a SimpleAddition transfer object and picked up for processing on the consumer thread.
Copy on write lists provide a very quick win in terms of removing synchronization. I believe that a significant use case is providing a thread safe list for java's Listener (AKA observer) pattern. Copy on write lists make the assumption that the list does not update frequently, and is mainly used for reading. If this is not the case, the overhead may be worse than synchronizing. java.util.concurrent.CopyOnWriteArrayList implements the List interface and infact is also a random access container (implements RandomAccess).