×
☰ See All Chapters

Thread Synchronization in Java

Threads in a program are in the same process memory, and therefore have access to the same memory. The biggest problem of allowing multiple threads sharing the same data set is that one operation in one thread could collide with another operation in other threads on the same data. When this happens, the result is un-desirable.

Let's use a bank application program as an example. Assuming that the program has two threads running, the two BankTeller threads have a reference to the same BankAccount object. In other words, these two threads share the same memory. The threads ran quickly enough that they did not ever block in the middle of running. However, consider we added a sleep() method  to thread, forcing the BankTeller threads to become blocked in the middle of a transaction:

thread-synchronization-in-java-0
 

Both you threads will receive $50.00 each, and account will still have $50.00. The bank could lose $50.00. When working with data-sensitive information such as a bank account balance, multiple threads accessing the data should take turns. For example, if a deposit is being made, no other transactions that affect the balance should be allowed until the deposit is finished. You can do this by synchronizing your threads.

Synchronization

Synchronization is a process of getting lock on the object so that only one thread can access that object. A running thread will enter either a blocked/waiting state when it tries to get the locked object. We can implement synchronization in two ways.

a. Method level synchronization.

b. Block level synchronization.

Synchronized method

To acquire the object's lock, just call a method that has been modified with the synchronized  keyword. While a thread is inside a synchronized method, all other threads that try to call it (or any other synchronized method) on the same instance have to wait. To release the lock and relinquish control of the object to the next waiting thread, the owner of the monitor simply returns from  the synchronized method.

 When one thread is having synchronized method, others (other thread, other methods) cannot access this synchronized method. But other methods of the same class can be accessible by any method or thread.  

thread-synchronization-in-java-1
 
thread-synchronization-in-java-2
 

Synchronized block

Suppose you want to block or synchronize only some part of the method (some lines of the method) then you can use synchronized block. Using this we can lock third party objects, whose methods are not synchronized.

thread-synchronization-in-java-3
 

static synchronization

Synchronized method and block works correctly when class having the synchronized method or block is instantiated only once per application. Means only one object of the class for the entire application. But there are situations we need multiple objects to work, in such situations making the synchronized method as static method will going work perfectly.

thread-synchronization-in-java-4
 

Deadlock

Deadlock can occur in a situation when a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread. Since, both threads are waiting for each other to release the lock, the condition is called deadlock.

                thread-synchronization-in-java-5
 

Key Note to the readers of Multithreading in java: A java web developer rarely writes a thread, Means everything comes multithreaded in web application environments. We just use them; we will not create any threads. In our experience as enterprise application developer, hardly got two to three chance to write a thread. So a java web developer should aware of threading in java, but not at all required to master in threading. Don’t burn your time in mastering multithreading. We have covered this topic enough to crack a tough java interview which is sufficient for a web developer. But if you are into develop standalone applications, we request you to go deep in multithreading after covering our topics.

 

 


All Chapters
Author