Thread Safety is very important factor when implementing multithreaded applications. Code Synchronization helps in preventing multiple threads to access same code at a given time. This tutorial explains how Code synchronization works and i have explained how Object Locks work with multiple threads.
Download PPT and example : http://java9s.com/core-java/thread-sa...
facebook: http://facebook.com/java9s
twitter: http://twitter.com/java9s
2. Need for Thread Safety
DataThread 1
Thread 2
Total Balance = 500
Withdraw amount
Wife = 450; Husband = 100
???
No Funds available
Java Application
JAVA9S.comJAVA9S.com
3. Need for Thread Safety
Happybus.com
Total Seats left: 3
No.of Seats wanted: 2 No.of Seats wanted: 2
When both of them get a chance to book at same time – then any one
of them will get a message that there are no sufficient seats left
JAVA9S.comJAVA9S.com
5. Code Synchronization for thread
safety• Code Synchronization helps in preventing multiple threads
executing a code simultaneously.
• Code Synchronization is implemented with the help of Locks.
• A thread that is trying to access the code that is marked as
Synchronized should acquire the lock from the object.
• Locks:
– Every object has a lock. Only one lock per object.
– Every class has a lock. Only one lock per loaded class file. (For static
methods)
JAVA9S.comJAVA9S.com
6. Synchronize - Few points
• Synchronize can only be applied for methods and
block of code.
• Synchronize cannot be applied for classes and
instance fields.
Syntax:
public synchronize void method(){ }
public static synchronize void method{…}
synchronize(this){…}
JAVA9S.comJAVA9S.com
7. Locks for Thread Safety
M1 M2 M3
Methodss s
M1 M2 M3
s s
M1 M2 M3
s
M1 M2 M3
s
Thread 1
Lock Board
Ob1
Ob2
Ob1 Ob2
Ob3 Ob4
Ob4
Thread 2
Lock Board
Ob1 Ob3
Ob41
2
3
4
1
3
2
When a thread holds lock of object to
Access synchronized method, any other thread
trying to access another synchronized method
Should wait till the lock is released
JAVA9S.comJAVA9S.com
8. Locks - An example
Locks for Objects and class files work as hotel
rooms.
A person entering the room should have a lock
and any other person who needs to occupy that
room should wait till the lock is released.
JAVA9S.comJAVA9S.com
9. Why only Single lock?
bookTickets()
cancelTickets()
int totalTickets
Both methods can impact the value of TotalTickets variable.
To avoid conflict and confusion, One lock per object helps to
Keep threads away from manipulating instance variables
through methods.
Both methods can impact the value of TotalTickets variable.
To avoid conflict and confusion, One lock per object helps to
Keep threads away from manipulating instance variables
through methods.
Remember to keep the instance variables marked as private
in multithread environment to avoid manipulating them by threads
Remember to keep the instance variables marked as private
in multithread environment to avoid manipulating them by threads
Unnecessary code synchronization will effect the applications performance.Unnecessary code synchronization will effect the applications performance.
JAVA9S.comJAVA9S.com