Tag: concurrency


Actor based parallelism using Scala and Akka : Part 2

Dispatchers in Akka Dispatchers are used to control the flow of execution in Akka - they can control how messages are being sent, delivered, received and processed. Based on the dispatching policy, dispatchers will route the incoming message to respe...

Actor based parallelism using Scala and Akka : Part 1

Threads and locks are popular programming constructs because they tend to mimic what the underlying hardware does. However, use of threads and low level concurrency features require developers to have in-depth knowledge of memory model and often inner ...

Scala Futures

A Future instance represents the result of an asynchronous computation. Future is a lightweight parallelism API provided as part of Scala core. Scala Futures are easily interoperable with Java Futures and executor-service implementations To use Future...

Java Memory Model

The Java memory model describes how threads in the Java programming language interact through memory. Together with the description of single-threaded execution of code, the memory model provides the semantics of the Java programming language.In single...

Spring : creating unique beans per thread

In stand-alone non-web Java applications, using Spring, the most commonly used bean scopes are singleton and prototype. If a bean scope is set to singleton (and by default bean scope is always singleton), the Spring container creates exactly one instan...

False Sharing

Most modern day computer architectures have a tiered memory structure. Cache memory is really fast (SRAM) and is also physically located very close to the processor (L1 is placed on CPU chip closest to processor, L2 is placed in between CPU and RAM, i...

CopyOnWriteArrayList and CopyOnWriteArraySet

CopyOnWriteArrayList CopyOnWriteArrayList uses an interesting technique to make it thread-safe without a need for synchronization - all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. This is or...

ConcurrentHashMap

A ConcurrentHashMap is a thread safe implementation of a hash table data structure. A ConcurrentHashMap has the same functional specifications as HashTable with the performance benchmarks comparable to a HashMap. Before we look into ConcurrentHashMap's...

Transfer Queue

A transfer queue is a special type of BlockingQueue (it implements BlockingQueue) where producers may optionally wait until consumers have consumed the corresponding element. Like other blocking queues, a TransferQueue may be capacity bounded. If so, ...

Common inter thread communication problems

Sleeping Barber Problem In computer science, the sleeping barber problem is a classic inter-process communication and synchronization problem between multiple operating system processes. The analogy is based upon a hypothetical barber shop with one bar...

Hardware Based Locking

CAS (Compare And Swap) : CAS ( Compare And Swap) is an atomic instruction used to implement optimistic locking. It compares the contents of a memory location with a given value and, only if they are the same, modifies the contents of that memory locati...

Executor Framework in Java

Executor is a framework provided by the JDK (starting 1.5) which simplifies the execution of tasks in asynchronous mode. At a high level, the Executor classes provide abstraction for assigning tasks to a pool of threads. The java.util.concurrent packag...

Synchronization tools for coordinating between threads

Apart from the low level synchronization abstractions (synchronized/Lock/Semaphore), Java also provides a bunch of high level abstractions for synchronizing activities of two or more threads. Let's have a closer look at some of them CountdownLatch Coun...

ThreadLocal

The ThreadLocal class helps us maintain 'per thread' semantics. Thus, even if two threads are executing the same code, and the code has a reference to a ThreadLocal variable, then the two threads cannot see each other's values. This is especially helpf...

Special thread features

Shutdown Hook : Shutdown Hooks are pre-registered (but yet-to-be-started) threads which are run by JVM when an application is shutting down. There can be more that one shutdown hooks registered with a JVM, JVM does not give any guarantee on the order i...

Points to consider while working on multithreaded code

In general, there are 4 main points to consider when it comes to dealing with multithreaded code : mutual exclusion caching reordering happens-before Let's examine each of them in detail : Mutual Exclusion : Mutual Exclusion essentially means that only...

Common concurrency problems

Starvation Starvation refers to a situation where a thread is unable to make any progress (or progressing very slowly) despite being in runnable state. Some of the common causes of starvation are : Other 'greedy' threads with higher priority are hogg...

Threads and Processes

What is a thread ? A thread is a kernel abstraction for scheduling work on the CPU. A thread is essentially an execution context which a CPU needs to execute a bunch of instructions. As a bare minimum coding construct, any thread implementation will ha...