Using Future and ConcurrentMap for a lazy cache

By dave

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

Simple uses of CountDownLatch

By dave

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 whilst the other thread calls . Once the count reaches zero the call returns and the latch is set. If the call to happens after...

Using Semaphore and CyclicBarrier in Java applications

By dave

In this example I show an usage of two more concurrent classes and ; 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...

BlockingQueue producer consumer example

By dave

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

Using thread local in Java

By dave

This article assumes that you are already familiar with concurrent programming and design. Java 1.5 introduced the concurrent library , which provides an extensive set of classes for dealing with concurrency issues, these sit alongside some existing classes that have been around since earlier times. I've noticed that some of the classes I mention here don't get used as often as...

Concurrent Maps and CopyOnWriteArrayList

By dave

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

These may be of interest

We use cookies to analyse traffic and to personalise content. We also embed Twitter and Youtube on some pages, these companies have their own privacy policies.

See the privacy policy and terms of use of this site should you need more information or wish to adjust your settings.