Category: Threads

Implement a Reentrant Lock in Java 0

Implement a Reentrant Lock in Java

Let’s try to implement our own custom Lock. For sake of simplicity we will not implement interruptible, non-interruptible, and timed forms of the lock /* * For sake of simplicity – assume custom lock...

How is Java Lock better than Synchronization 0

How is Java Lock better than Synchronization

Here are few benefits of using Java’s Lock API over Synchronization 1.Java Lock API provides more visibility and options for locking, unlike synchronized where a thread might end up waiting indefinitely for the lock,...

When should we use Volatile variables 0

When should we use Volatile variables

In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the...

How to catch exception thrown by a thread 0

How to catch exception thrown by a thread

In this example we will see how to use Thread.UncaughtExceptionHandler. UncaughtExceptionHandler is used as a way to provide an elegant way to handle Runtime Exceptions which are not handled otherwise in the programs. As...

How do we implement semaphore in Java 0

How do we implement semaphore in Java

A semaphore is a particular lock that allows more than one thread to access the target object. It’s like having more keys, so that many people can unlock the door. This is how a...