Sunday, 7 May 2017

16. Synchronized block


1. What is Synchronized block in Thread
1.       If very few lines of code required synchronization then it's not recommended to declare method as synchronized we have to enclose those few line of the code by using synchronized block.
2.       The main advantage of synchronized block over synchronized method is it reduces waiting time of thread and improves performance of the system.
3.       Syntax of synchronized block
Synchronized(this){}
4.       We can declare synchronized block as following ways
To get lock of current object
To get lock of particular object
To get class level lock
Synchronized(this){
.........................
}
Synchronized(b){
.........................
}
Synchronized(Display.class){
.........................
}
If a thread got lock of current
object, then only it allows to execute this area
If a thread got lock of object ‘b’
, then only it allows to execute
this area
If a thread get class level lock
Of ‘Display’ class then only its
Allowed to execute this area.
5.       Synchronized block contains an object or class , means for current thread executing Synchronized block first it will lock the object or lock the class.
6.       For next thread before entering into Synchronized block it will check lock is available or not, if not available it will go to wait state.
Synchronized(new String(“a”)){
Sop(name);
}
Synchronized(“a”){
Sop(name);
}
Synchronized(String.class){
Sop(name);
}
For each thread t1, t2 checks
object has locked or not.
All threads will enter same time because every time new object
Will be created and checks the
Lock is available or not.
For each thread t1, t2 checks
object has locked or not.
At a time single thread will
Execute because “a” is mutable object , so if t2 came already
Lock on “a” object
For each thread t1, t2 checks
object has locked or not.
At a time single thread will
Execute because class level
Lock , for t2 it will check
Class level lock is available or
Not, if not it will go to wait
State.
Multiple Threads enter (all
threads )
Single thread enter at a time
Single Thread enter at a time
Synchronized(this)
Sop(name);
}
Synchronized(d){
Sop(name);
}
Synchronized(Display.class){
Sop(name);
}
Single thread enter at a time,
Current object instance
Synchronized methods also
Will not execute
Single thread enter at a time,
Lock on d object, so d instance
Synchronized methods also
Not execute
Single Thread enter at a time,
Lock on Display class, so Synchronized static methods
Will not execute
If two threads t1, t2 are calling
D1.wish(); - this = d1
D2.wiah(); - this = d2
T1 need lock of d1, t2 need lock
Of d2 ,
So both threads will execute
In this case.
If two threads t1, t2 are calling
D1.wish(); - d = d1
D2.wiah(); - d = d2
T1 need lock of d1, t2 need lock
Of d2 ,
So both threads will execute
In this case.
If two threads t1, t2 are calling
D1.wish(); - class = Display
D2.wiah(); - class = Display
T1 need lock of Display class
, t2 need lock
Of same Display class ,
So one thread will execute
At a time.
2. Types of locks in Thread
1.       In Threads they are 2 types of locks available
a.       Object level Lock
b.      Class level Lock
2.       Lock is available for objects and class but not applicable for primitive data types.
Work
Not Work (compile error)
Not Work (compile error)
Integer i = 10
Synchronized(i)
Sop(name);
}
Int i=10
Synchronized(i){
Sop(name);
}
Synchronized(10){
Sop(name);
}

No comments:

Post a Comment

3. Java Program to create Binary Tree