What is Thread Synchronization
By Ramakrishna on Dec 10, 2008 in Thread Synchronization
THREAD SYNCHRONIZATION
There will be no problem, Until when the thread uses their own data and method provided inside their run() methods.
What happens when they try to use data and methods outside themselves,
On that Occasions, they may compete for the same resources and may lead to serious problems. For example, one thread may try to read a record from a file while another is still writing to the same file . Depending on the situation, we may get strange results. Java enables us to overcome this problem using a technique known as synchronization.
In case of Java, the keyword synchronized helps to solve problems by keeping a watch on such locations. For example, the method that will read information from a file and the method that will update the same file may be declared as synchronized. Example:
Syntax:
synchronized void update()
{
…………….
……………. //code here is synchronized
…………….
}
When we declare a method synchronized, Java creates a “monitor” and hands it over to the thread that calls the method first time. As long as the thread holds the monitor, no other thread can enter the synchronized section of code. A monitor is like a key and the thread that holds the key can only open the lock.
It is also possible to mark a block of code as synchronized as shown below.
synchronized (lock-object)
{
…………….
……………. //statements to be synchronized
…………….
}
(OR) Simply to Say
A Thread Synchronization is a Coordination between different threads is known as thread synchronization. When two or more threads need to operate on the same pieces of the same data structure or their exist a producer/consumer relationship among the threads, then these threads need to be synchronized.
A synchronized method will not allow multiple threads in it.
Sample example for thread synchronized
//This program uses a synchronized block
class Callme {
void call(String msg) {
System.out.print(”["+msg);
try {
Thread.sleep(1000);
}catch(InterruptedException e)
{ System.out.println("Interrupted"); }
System.out.println("]“);
}
}
class Caller implements Runnable {
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s) {
target = targ;
msg =s;
t=new Thread(this);
t.start();
}
//synchronize call to call()
public void run() {
synchronized(target) { //synchronized block
target.call(msg);
}
}
}
class Synch1{
public static void main(String args[]) {
Callme target=new Callme();
Caller ob1 = new Caller(target, “Hello”);
Caller ob2 = new Caller(target, “Synchronized”);
Caller ob3 = new Caller(target, “World”);
//wait for threads to end
try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}catch(InterruptedException e) {
System.out.println(”Interrupted “);
}
}
}
Here, the call() method is not modified by synchronized. Instead, the synchronized statement is used inside Caller’s run() method. This causes the same correct output as the preceding example, because each thread waits for the prior one to finish before proceeding.
