Category: 05. Java Concurrency
-
AtomicInteger Class
A java.util.concurrent.atomic.AtomicInteger class provides operations on underlying int value that can be read and written atomically, and also contains advanced atomic operations. AtomicInteger supports atomic operations on underlying int variable. It have get and set methods that work like reads and writes on volatile variables. That is, a set has a happens-before relationship with any…
-
Condition Interface
A java.util.concurrent.locks.Condition interface provides a thread ability to suspend its execution, until the given condition is true. A Condition object is necessarily bound to a Lock and to be obtained using the newCondition() method. Condition Methods Following is the list of important methods available in the Condition class. Sr.No. Method & Description 1 public void…
-
ReadWriteLock Interface
A java.util.concurrent.locks.ReadWriteLock interface allows multiple threads to read at a time but only one thread can write at a time. Lock Methods Following is the list of important methods available in the Lock class. Sr.No. Method & Description 1 public Lock readLock()Returns the lock used for reading. 2 public Lock writeLock()Returns the lock used for…
-
Lock Interface
A java.util.concurrent.locks.Lock interface is used to as a thread synchronization mechanism similar to synchronized blocks. New Locking mechanism is more flexible and provides more options than a synchronized block. Main differences between a Lock and a synchronized block are following − Lock Methods Following is the list of important methods available in the Lock class.…
-
ThreadLocalRandom Class
A java.util.concurrent.ThreadLocalRandom is a utility class introduced from jdk 1.7 onwards and is useful when multiple threads or ForkJoinTasks are required to generate random numbers. It improves performance and have less contention than Math.random() method. ThreadLocalRandom Methods Following is the list of important methods available in the ThreadLocalRandom class. Sr.No. Method & Description 1 public…
-
ThreadLocal Class
The ThreadLocal class is used to create thread local variables which can only be read and written by the same thread. For example, if two threads are accessing code having reference to same threadLocal variable then each thread will not see any modification to threadLocal variable done by other thread. ThreadLocal Methods Following is the…
-
Deadlock
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in different order. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock, or…
-
Synchronization
Multithreading Example with Synchronization Here is the same example which prints counter value in sequence and every time we run it, it produces the same result. Example This produces the same result every time you run this program − Output
-
Interthread Communication
If you are aware of interprocess communication then it will be easy for you to understand interthread communication. Interthread communication is important when you develop an application where two or more threads exchange some information. There are three simple methods and a little trick which makes thread communication possible. All the three methods are listed…
-
Major Operations
Core Java provides complete control over multithreaded program. You can develop a multithreaded program which can be suspended, resumed, or stopped completely based on your requirements. There are various static methods which you can use on thread objects to control their behavior. Following table lists down those methods − Sr.No. Method & Description 1 public…