INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
Thread synchronization
DISCOVER . LEARN . EMPOWER
Lecture Objectives
In this lecture, we will discuss:
•Thread synchronization
2
Synchronization
• Threads often need to share data.
• Different threads shouldn’t try to access and change the same data at the same
time
• Threads must therefore be synchronized
• Key to synchronization is the concept of the monitor (also called a semaphore).
• For example, imagine a Java application where one thread (which let us assume as
Producer) writes data to a data structure, while a second thread (consider this as
Consumer) reads data from the data structure
Synchronize multiple threads
•Make sure that only one thread can access the resource at a given point in time.
•Implemented using a concept called monitors.
•Each object in Java is associated with a monitor, which a thread can lock or
unlock. Only one thread at a time may hold a lock on a monitor.
Thread Synchronization
There are two types of thread synchronization mutual exclusive and inter-thread
communication.
•Mutual Exclusive
•Synchronized method.
•Synchronized block.
•static synchronization.
•Cooperation (Inter-thread communication in java)
Mutual Exclusive
Mutual Exclusive helps keep threads from interfering with one another while sharing
data. This can be done by three ways in java:
by synchronized method
by synchronized block
by static synchronization
Concept of Lock in Java
Synchronization is built around an internal entity known as the lock or monitor.
Every object has an 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.
Java synchronized method
If you declare any method as synchronized, it is known as synchronized method.
Synchronized method is used to lock an object for any shared resource.
When a thread invokes a synchronized method, it automatically acquires the lock for
that object and releases it when the thread completes its task.
Example
//java synchronized method
class Table{
synchronized void printTable(int n){//synchronized method
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
} } }
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);
} }
Output:
5
public class TestSynchronization2{
10
public static void main(String args[]){ 15
Table obj = new Table();//only one object 20
25
MyThread1 t1=new MyThread1(obj);
100
MyThread2 t2=new MyThread2(obj); 200
t1.start(); 300
400
t2.start();
500
} }
QUIZ:
What is synchronization in reference to a thread?
a) It is a process of handling situations when two or more threads need
access to a shared resource.
b) It is a process by which many thread are able to access same shared
resource simultaneously.
c) It is a process by which a method is able to access many different
threads simultaneously.
d) It is a method that allow to many threads to access any information
require.
Summary:
In this session, you were able to :
•Learn about Thread Synchronization
(to be continued……)
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill Companies
3. John P. Flynt Thomson, Java Programming.
Video Links:
https://youtu.be/RH7G-N2pa8M
Reference Links:
http://tutorials.jenkov.com/java-concurrency/synchronized.html
https://www.geeksforgeeks.org/method-block-synchronization-java/
https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html
https://www.journaldev.com/31514/java-synchronized-keyword-method-block
https://www.javatpoint.com/synchronization-in-java
https://www.geeksforgeeks.org/synchronized-in-java/
THANK YOU