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

Answers were Sorted based on User's Feedback



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

Answer / vikki

There are two ways to syanchronize access to an object

1st one is using synchronized keyword for example

Synchronized type methodname(arg-list)
{
//synchrinized method body
}

2nd way s using synchronized block

synchronized(object)
{
//synchronized statement

}

Is This Answer Correct ?    30 Yes 0 No

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

Answer / ajay tiwary

The way you can synchronize the block is:-

synchronized(dummy object)
{
//synchronized statement

}
In dummy object you can use this or u can create a dummy
object by using new Keyword.

What answer Vikki is posted as the first one is not the way
of synchronizing the block but it's a way to synchronizing
a method.

Is This Answer Correct ?    5 Yes 0 No

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

Answer / 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

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

Answer / popol83

I think using mutex is the second way.
Because synchronizing an object is useless

Is This Answer Correct ?    1 Yes 0 No

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

Answer / guest

bu using thread in your main program

Is This Answer Correct ?    2 Yes 16 No

Post New Answer

More Core Java Interview Questions

What is math exp in java?

0 Answers  


In what ways you can handle exception ?

3 Answers  


What 5 doubled?

0 Answers  


Is a copy constructor?

0 Answers  


What is run-time class and system class? what is their purpose?

1 Answers  






What is the use of TL?

0 Answers  


can used Protected Class outside Function.?

0 Answers   HCL,


What is UNICODE?

3 Answers  


Can we change the value of static variable?

0 Answers  


What is numeric promotion?

1 Answers  


How big is a boolean?

0 Answers  


Difference between final and effectively final ?

0 Answers  


Categories