Previous | Home | Next |
If two or more threads are sharing common resource for example a file. One thread is reading from the file while another thread writes to the file. Both Threads try to access that shared resource. This arises Race Condition .
The thread win the race, will access to file first. This may cause read or write wrong values and may malfunction or corrupt he program. So one thread should reach one time to perform action..
Synchronization is way to achieve this condition. Key of synchronization is concept of monitor which called semaphore also. This semaphore puts lock on thread so that one thread can get access at a time to the shared resources.
Synchronization achieved in Java by two ways:
- Using synchronized methods
- Using Synchronized statements
In Java all objects have their monitor. To enter the monitor we put synchronized keyword on method. of the class that accessed by multiple Threads. In following example we will see what occur if don't use synchrinization
Example
/* * implementation of Synchronization */ package threadexample; class Shared { void call(String str) //method that can baccessd by multiple threads { System.out.println("["+str); try { Thread.sleep(500); }catch(InterruptedException e) { System.out.println("Interrupted"); } } } class Caller implements Runnable { String msg; Shared obj; Thread t; public Caller(Shared obj1,String s ) { t=new Thread(this); obj=obj1; msg=s; t.start(); } public void run() { obj.call(msg); } } public class implementsynchronization { public static void main(String[] args) { Shared shr=new Shared(); Caller thrd1=new Caller(shr,"Hello"); Caller thrd2=new Caller(shr,"My"); Caller thrd3=new Caller(shr,"Friends"); try { thrd1.t.join(); thrd2.t.join(); thrd3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); } } }
Output:
run: [Hello[Friends[My] ] ]
As we can see call method from Shared class accessed by multiple threads. Our out doesn't came as expected. Now put synchronized keyword with call method like:
synchronized void call(String str)
After using synchronized word above programm and it's out put looks like:
Example
/* * implementation of Synchronization */ package threadexample; class Shared { synchronized void call(String str) //method that can baccessd by multiple threads { System.out.println("["+str); try { Thread.sleep(500); }catch(InterruptedException e) { System.out.println("Interrupted"); } } } class Caller implements Runnable { String msg; Shared obj; Thread t; public Caller(Shared obj1,String s ) { t=new Thread(this); obj=obj1; msg=s; t.start(); } public void run() { obj.call(msg); } } public class implementsynchronization { public static void main(String[] args) { Shared shr=new Shared(); Caller thrd1=new Caller(shr,"Hello"); Caller thrd2=new Caller(shr,"My"); Caller thrd3=new Caller(shr,"Friends"); try { thrd1.t.join(); thrd2.t.join(); thrd3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); } } }
Output:
[Hello] [My] [Friends]
Synchronized method can't be used in all case such as we wants to achieve synchronization on the objects which are not designed for the multithreaded access. We can achieve synchronization by help of synchronized statement. Synchronized statement can be used as follows.
synchronized(object) { //statement to be synchronized }
Example we shown for synchronized method can be modified as:
/* /* * SUing synchronize statement */ package threadexample; /** * * @author R4R */ class Shared { void call(String str) //method that can baccessd by multiple threads { System.out.print("["+str); try { Thread.sleep(1000); }catch(InterruptedException e) { System.out.println("Interrupted"); } System.out.println("]"); } } class Caller implements Runnable { String msg; Shared obj; Thread t; public Caller(Shared obj1,String s ) { t=new Thread(this); obj=obj1; msg=s; t.start(); } public void run() { synchronized(obj){ obj.call(msg); } } } public class implementsyncstmt { public static void main(String[] args) { Shared shr=new Shared(); Caller thrd1=new Caller(shr,"Hello"); Caller thrd2=new Caller(shr,"My"); Caller thrd3=new Caller(shr,"Friends"); try { thrd1.t.join(); thrd2.t.join(); thrd3.t.join(); } catch(InterruptedException e) { System.out.println("Interrupted"); } } }
Output:
[Hello] [My] [Friends]
Previous | Home | Next |