When two or more threads need to access shared memory, then there is some way to ensure that the access to the resource will be by only one thread at a time.
Synchronization
• When two or more threads need to access shared
memory, then there is some way to ensure that the access to the resource will
be by only one thread at a time. The process of ensuring one access at a time
by one thread is called synchronization. The synchronization is the concept
which is based on monitor. Monitor is used as mutually exclusive lock or mutex.
When a thread owns this monitor at a time then the other threads can not access
the resources. Other threads have to be there in waiting state.
• In
Java every object has implicit monitor associated with it. For entering in
object's monitor, the method is associated with a keyword synchronized. When a
particular method is in synchronized state then all other threads have to be
there in waiting state.
• There
are two ways to achieve the synchronization -
1. Using
Synchronized Methods 2. Using Synchronized Blocks (Statements).
Let us
make the method synchronized to achieve the synchronization by using following
Java program -
1. Using Synchronized Method
class
Test
{
synchronized
void display(int num)
System.out.println("\nTable
for "+num);
{
System.out.print("
"+num*i);
}
System.out.print("\nEnd
of Table");
try
{
Thread.sleep(1000);
}catch(Exception
e){}
}
}
class A
extends Thread
{
Test
th1;
A(Test
t)
{
th1=t;
}
public
void run()
{
th1.display(2);
}
}
class B
extends Thread
{
Test
th2;
B(Test
t)
{
th2=t;
}
public
void run()
{
th2.display(100);
}
}
class
MySynThread
{
public
static void main(String args[])
{
Test
obj=new Test();
A t1=new
A(obj);
B t2=new
B(obj);
t1.start();
t2.start();
}
}
Output
Program
Explanation:
• In
above program we have written one class named Test. Inside this class the
synchronized method named display is written. This method displays the table of
numbers.
• We
have written two more classes named A and B for executing the thread. The
constructors for class A and Class B are written and to initialize the instance
variables of class Test as th1 (as a variable for class A)and th2(as a variable
for class B)
• Inside
the run methods of these classes we have passed number 2 and 10 respectively
and display method is invoked.
• The
display method executes firstly for thread t1 and then for t2 in synchronized
manner.
2. Using Synchronized Block
• When
we want to achieve synchronization using the synchronized block then create a
block of code and mark it as synchronized.
• Synchronized
statements must specify the object that provides the intrinsic lock.
Syntax
The
syntax for using the synchronized block is
synchronized(object
reference)
{
statement;
statement;
//block of code to be synchronized
.
.
.
}
Java Program
class
Test
{
void
display(int num)
{
Synchronized(this)
{
System.out.println(“\n
Table for”+num);
for( int
i=1;i<=10;i++)
{
System.out.println(“
”+num*i);
}
System.out.println(“\n
End of Table”);
try
{
Thread.sleep
(1000);
} catch(Exception
e){}
}
}
class A extends
Thread
{
Test
th1;
A(Test
t)
{
th1=t;
}
public
void run()
{
th1.display(2);
}
}
class B
extends Thread
{
Test
th2;
B(Test
t)
{
th2=t;
}
public
void run()
{
th2.display(100);
}
}
class
MySynThreadBlock
{
public
static void main(String args[])
{
Test
obj=new Test();
A t1=new
A(obj);
B t2=new
B(obj);
t1.start();
t2.start();
}
}
Output
Points
to Remember about Synchronization
1. Only
methods can be synchronized but the variables and classes can not be
synchronized.
2. Each
object has one lock.
3. A
class contains several methods and all methods need not be synchronized.
4. If
two threads in a class wants to execute synchronized methods and both the
methods are using the same instance of a class then only one thread can access
the synchronized method at a time.
5. We
can not synchronize the constructors.
6. A
thread can acquire more than one lock.
Review
Questions
1.What
is thread synchronization ? Discuss with an example.
2. What
is synchronization? Explain the different types of synchronization in java.
Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : types with Example Java Programs | Multithreading - Synchronization
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation