Synchronization in Java
Synchronization in Java
Mutual Exclusive:
Synchronized block.
Static synchronization.
Lock in Java:
Synchronization is built around an internal entity known as
the lock or monitor. Every object has a lock associated with it.
By convention, a thread that needs consistent access to an
object's fields has to acquire the object's lock before accessing
them, and then release the lock when it's done with them.
From Java 5 the package java.util.concurrent.locks contains
several lock implementations.
Synchronized method:
When we declare a synchronized keyword in the header of a
method, it is called synchronized method in Java.
Using the synchronized keyword, we can synchronize all the
methods of any class.
When a method is declared as synchronized, JVM creates a
monitor (lock). To enter the monitor, the synchronized
method is called. The thread that calls the synchronized
method first, acquires object lock.
If the object lock is not available, the calling thread is blocked
and it has to wait until the lock becomes available.
As long as thread holds lock (monitor), no other thread can
enter the synchronized method and will have to wait for the
current thread to release the lock on the same object.
Once a thread completes the execution of code inside the
synchronized method, it releases object lock and allows other
thread waiting for this lock to proceed.
That is once a thread completes its work using synchronized
method, it will hand over to the next thread that is ready to use
the same resource.
Syntax:
synchronized data_type method_name()
{
// Code to be synchronized.
}
}
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:
5
100
10
200
15
300
20
400
25
500
}
}
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
Synchronized block:
Synchronized block in Java is another way of managing the
execution of threads. It is mainly used to perform
synchronization on a certain block of code or statements
inside the method.
Synchronizing a block of code is more powerful
than synchronized method.
For example, suppose there are 30 lines of code in a method,
but we want to synchronize only 5 lines of code. In this case,
we should use a synchronized block.
If we place all the codes of the method in the synchronized
block, it will work the same as the synchronized method.
Syntax:
synchronized(object)
{
// statements or codes to be synchronized.
}
}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}
Static Synchronization
class Table
{
synchronized static void printTable(int n){
for(int i=1;i<=10;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){}
}
}
}
class MyThread1 extends Thread{
public void run(){
Table.printTable(1);
}
}
class MyThread2 extends Thread{
public void run(){
Table.printTable(10);
}
}
class MyThread3 extends Thread{
public void run(){
Table.printTable(100);
}
}
class MyThread4 extends Thread{
public void run(){
Table.printTable(1000);
}
}
public class TestSynchronization4{
public static void main(String t[]){
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
MyThread3 t3=new MyThread3();
MyThread4 t4=new MyThread4();
t1.start();
t2.start();
t3.start();
t4.start();
}
}
Output:
1
2
3
4
5
6
7
8
9
10
10
20
30
40
50
60
70
80
90
100
100
200
300
400
500
600
700
800
900
1000
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
Inter-thread Communication in Java:
Inter-thread communication in Java is a technique through
which multiple threads communicate with each other.
It provides an efficient way through which more than one
thread communicate with each other by reducing CPU idle
time. CPU idle time is a process in which CPU cycles are not
wasted.
When more than one threads are executing simultaneously,
sometimes they need to communicate with each other by
exchanging information with each other. A thread exchanges
information before or after it changes its state.
There are several situations where communication between
threads is important.
For example, suppose that there are two threads A and B.
Thread B uses data produced by Thread A and performs its
task.
If Thread B waits for Thread A to produce data, it will waste
many CPU cycles. But if threads A and B communicate with
each other when they have completed their tasks, they do not
have to wait and check each other’s status every time.
Thus, CPU cycles will not waste. This type of information
exchanging between threads is called inter-thread
communication in Java.
notify() Method:
The notify() method wakes up a single thread that called
wait() method on the same object. If more than one thread is
waiting, this method will awake one of them. The general
syntax to call notify() method is as follows:
Syntax:
public final void notify()
notifyAll() Method:
The notifyAll() method is used to wake up all threads that
called wait() method on the same object. The thread having
the highest priority will run first.
Syntax:
public final void notifyAll()
Understanding the process of inter-thread
communication: