
Thread synchronization prevents another thread from accessing an item that is currently being worked on by another thread.
If thread T1 is working on object O1, we prohibit thread T2 from accessing object O1 during the thread synchronization procedure. Thread synchronization is all this is.
The “synchronized” modifier is used to establish synchronization. It only applies to methods and blocks; classes and variables are not included.
When we declare a method or block as synchronized, only one thread may execute it on the supplied object at a moment.
Table of Contents
Need of Thread Synchronization
Because several objects might act on the same item when working in a multithreading environment, there is a risk of data inconsistency. Thread synchronization is used to solve the problem of data inconsistency.
However, it may increase thread waiting time, which may have an impact on system performance. As a result, you should only use the synchronized keyword if you have a necessity.
When synchronization is used, one thread locks the object, preventing the second thread from accessing it until the first thread has exited.
Synchronized Method for Thread Synchronization
When one thread runs a synchronized method on an object, the other thread is unable to execute a synchronized method on the same object. Non-synchronized methods, on the other hand, can be executed by other threads.
public synchronized void doWork() {
}
Synchronized Block for Thread Synchronization
Declare those sections as synchronized when you need to synchronize a set of statements. The synchronized block minimizes the thread’s waiting time and enhances the system’s performance.
You may use synchronized block to lock the current object like follows:
synchronized (this) {
}
If you want to lock the any other object you can use synchronized block as follows:
synchronized (object) {
}
You can only use the synchronized block notion for classes and objects; if you use it for basic types, you’ll get a compile time error.
Class Level lock
A thread must have class level lock in order to run a static synchronized method. If a thread performs a static synchronized method, the remaining threads cannot execute any static synchronized method of the class at the same time. The following methods can be executed by the remaining threads:
- Normal static methods
- Normal Instance methods
- Synchronized instance methods
Class level lock in synchronized block
To obtain class level lock, we may declare synchronized block as follows.
synchronized (<class-name>.class) {
}
Note: Synchronization is only useful when many threads are acting on the same object. If several threads act on numerous objects, synchronization has no impact. At the same moment, a thread can acquire several locks from distinct objects.
Thread Communication
We sometimes need to establish communication between two threads when programming. The wait(), notify(), and notify() methods are used to establish such communication. The thread that requires an update calls the wait() method, while the thread responsible for updating it calls the notify() method.
The wait(), notify(), and notify() methods are accessible in the Object class; if a thread wishes to use these methods, it must first acquire the object’s lock and be in the synchronized region.
These methods can only be called from a synchronized region; otherwise, an IllegalMonitorStateException will be thrown.
When a thread invokes the wait() method, the lock is instantly released and the thread is placed in the wait state. The thread releases just the current object’s locks, not all locks, when the notify() or notifyAll() methods are invoked. And also it won’t be able to release lock immediately.
notify() and notifyAll() methods
We can only notify one waiting thread with notify(), but we can’t predict which waiting thread will be alerted. The remaining threads will be held until further notification is received.
When notifyAll() is used, all waiting threads are notified , but they are executed one by one.
Deadlock of Threads
When one thread locks an object and waits for another thread to be freed by another object, and the second thread does the same, both threads will wait indefinitely. This is referred to as thread deadlock.
In other words, a deadlock occurs when two threads are stuck waiting for each other indefinitely. Any further execution is halted and the software comes to a halt in the event of a thread deadlock.
Avoid deadlocks in a Program
It is determined by the programming logic you create. There is no precise strategy for resolving the deadlock. You should be cautious when using the synchronized keyword in programming logic since synchronized keyword is the only source of deadlocks.
Daemon Threads
JVM creates daemon threads, which operate in the background and provide services to others. The garbage collector, for example, is always running in the background, collecting unneeded objects and freeing up memory.
The Daemon thread’s primary goal is to assist the non-daemon thread. Only before beginning a thread can you modify the Daemon nature. If you try to modify after starting a thread, you’ll get a “IllegalThreadStateException” runtime error.
Note: When the final non-daemon thread is terminated, all daemon threads will be killed automatically.
Good Job, Nice Article…