What are the two ways you can synchronize a block of code?

Answer Posted / supriyo pal

There are two ways to synchronized the execution of code:
1. Synchronized Methods
2. Synchronized Blocks (Statements)

Synchronized Methods
If any method is specified with the keyword synchronized then this method of an object is only executed by one thread at a time. A any thread want to execute the synchronized method, firstly it has to obtain the objects lock. Acquire the method is simply by calling the method. If the lock is already held by another thread, then calling thread has to wait.

public class SynThread{
public static void main(String args[]){
Share s=new Share();
MyThread m1=new MyThread(s,"Thread1");
MyThread m2=new MyThread(s,"Thread2");
MyThread m3=new MyThread(s,"Thread3");
}
}
class MyThread extends Thread{
Share s;
MyThread(Share s,String str){
super(str);
this.s=s;
start();
}
public void run(){
s.doword(Thread.currentThread().getName());
}
}
class Share{
public synchronized void doword(String str){
for(int i=0;i<5;i++){
System.out.println("Started :"+str);
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
}

Synchronized Blocks (Statements)
A synchronized statement is another way to create synchronized code. Synchronized statements must specify the object that provides the intrinsic lock. The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.
General form of synchronized block is:
synchronized (object reference expression)
{
code block..
}

public class SynStatement{
public static void main(String args[]){
Share s=new Share();
MyThread m1=new MyThread(s,"Thread1");
MyThread m2=new MyThread(s,"Thread2");
MyThread m3=new MyThread(s,"Thread3");
}
}
class MyThread extends Thread{
Share s;
MyThread(Share s,String str){
super(str);
this.s=s;
start();
}
public void run(){
s.doword(Thread.currentThread().getName());
}
}
class Share{
public void doword(String str){
synchronized(this){
for(int i=0;i<5;i++){
System.out.println("Started :"+str);
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
}
}


http://www.roseindia.net/java/thread/synchronization.shtml

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why does abstract class have constructor?

778


What is the scope or life time of instance variables?

895


Explain the features of interfaces in java?

768


What is java instanceof operator?

812


Which is faster string or stringbuilder?

726


What is callablestatement? How you can call stored procedure to pass in parameter?

777


What are java packages? What's the significance of packages?

890


Is stringwriter thread safe?

756


Can you extend main method in java?

862


What is character in data type?

763


What is the implementation of destroy method in java. Is it native or java code?

751


What is used of static keyword in java?

796


How does thread synchronization occurs inside a monitor?

798


we have syntax like for(int var : arrayName) this syntax is to find whether a number is in the array or not.but i want to know how to find that number's location.

1825


How many types of methods are there?

764