Java ReentrantLock

ReentrantLock Introduction Lock provides a tool to control access to a shared resource in a multi-threaded environment. A lock provides access to only one thread at a time to the shared resource. Using synchronized methods and statements provides access to an implicit lock for an object. However in certain scenarios we might need access explicitly …

Java ReentrantLock Read More »

Java Exchanger

Exchanger Introduction Exchanger is a synchronization point in which threads can pair and exchange objects. Exchanger simplifies data exchange between two threads. Exchanger waits till two threads reach the synchronization point and then exchanges the data provided by the threads. Java Exchanger Class Java support Exchanger synchronization object. Exchanger is a generic class and support …

Java Exchanger Read More »

Java Atomic Variables

Atomic Operations Introduction Traditional multi-threading approaches use locks to protect shared resources.  Synchronization objects like Semaphores provide mechanisms for the programmer to write code that doesn’t modify a shared resource concurrently. The synchronization approaches block other threads when one of the thread is modifying a shared resource. Obviously blocked threads are not doing meaningful work …

Java Atomic Variables Read More »

Java CyclicBarrier

CyclicBarrier Introduction CyclicBarrier is a synchronization object that will release when a given number of threads are waiting on it. CyclicBarrier is initialized with a count that indicates the number of threads that must wait on this barrier. CyclicBarrier is useful in applications where threads needs to wait for each other.  Java CyclicBarrier Class Java …

Java CyclicBarrier Read More »

Java CountDownLatch

CountDownLatch Introduction CountDownLatch is a synchronization object that allows a thread to wait till certain events occur in other threads. We can make the current thread to wait until few dependent threads have completed their operation. CountDownLatch starts with a initial count. Thread that needs to wait, blocks until the count reaches to zero. Dependent …

Java CountDownLatch Read More »

Java Semaphore

Semaphore Introduction Semaphore is a synchronization primitive that can be used to ensure that only one thread can access a shared resource at any point of time. Semaphore guards critical sections of the code by providing the necessary signalling and prevents any race conditions. Semaphore controls access to shared resources by using counters. If the …

Java Semaphore Read More »

Java Callable Future Example

Java concurrent API support Callable and Future interfaces to implement threads that can return a value. One simple use case for a Callable object is to return status (Success/ Failure) of thread execution to the invoking object. More practical use cases include perform number crunching operations simultaneously wherein partial results could be return back to the …

Java Callable Future Example Read More »

Java Properties Example

Handling persistent properties is an important aspect in software design to prevent hard coding values in code. Properties allow the customization of values like username, password, port numbers etc. during run time and provides flexibility to the implementation. Java support simple APIs to handle read and store properties. This article provides an introduction to working …

Java Properties Example Read More »

Java Reflection Tutorial

Java reflection is an advanced feature and is the ability to examine or modify the run-time behavior of applications.  Java reflection is primarily used in applications like test tools, debuggers, class browsers etc. Reflection provides the ability to examine private members of a class and also has associated performance overhead since the types are dynamically …

Java Reflection Tutorial Read More »

Java UDP Sockets

TCP/IP sockets explained in this article is a reliable form of communication. However there is additional overhead involved in terms of error handling, congestion control etc. in TCP/IP communication. The other alternative available is to use UDP (User Datagram Protocol). UDP protocol doesn’t guarantee that the message sent is transmitted reliably. Since UDP involves less overhead …

Java UDP Sockets Read More »