wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Doubt in synchronized.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Doubt in synchronized...." Watch "Doubt in synchronized...." New topic

Doubt in synchronized....

sridhar Ranganathan

Joined: Feb 06, 2008
Posts: 29
What is synchronized block? How it will work?Give me simple ex.
Why synchronized is not working in this below pgm.

public class test18 extends Thread

String setString="";
test18(String set)

public void run()


}catch(InterruptedException op)
System.out.println("Exception : "+op);
private synchronized void callLogic() throws InterruptedException
for(int i=0;i<100;i++)




public static void main(String[] args)
test18 ob1=new test18("A");
test18 ob2=new test18("B");
test18 ob3=new test18("C");



output :--->


Synchronized means :- It will allow only one thread at time right? That mean output will come
AAAAAAAAAAAAAAAAAAAA 100times and BBBBBBBBBBBBBBBBB 100 times and CCCCCCCCCC times. What is the problem here,

Thanks & Regards,
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210


Synchronization is to manage access to data from multiple threads.
Some classic example is web hit counter, where you have 1 object that keep track of the number of visits but you have multiple threads of request/servlet which tries to increment the same instance of the counter object.

The example that you have is not working because each test has their own instance of data that they manipulate. So what you see is behavior of standard thread-based operation where you have multiple threads doing their own thing that are being managed by JVM where each thread has its own chance to run.

Do some Google search, there are plenty of tutorials on concurrent programming which give clear example what synchronization do.
[ May 08, 2008: Message edited by: Wirianto Djunaidi ]
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3216
In Java programming, each object has a lock. A thread can acquire the lock for an object by using the synchronized keyword. The synchronized keyword can be applied in method level (coarse grained lock � can affect performance adversely) or block level of code (fine grained lock). Often using a lock on a method level is too coarse. Why lock up a piece of code that does not access any shared resources by locking up an entire method. Since each object has a lock, dummy objects can be created to implement block level synchronization. The block level is more efficient because it does not lock the whole method.

Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Originally posted by arulk pillai:
Often using a lock on a method level is too coarse.

In general a fine grained lock is good to have when the lock contention is high.
However, when the lock contention is less then it is better to keep it simple.
Fine grained locks make the code a little cluttered/hard to decide the sync boundaries and has scope for bugs that are hard to debug.
It is like worrying about saving resource which no one else is actually using.
[ May 09, 2008: Message edited by: Nitesh Kant ]

apigee, a better way to API!
I agree. Here's the link: http://aspose.com/file-tools
subject: Doubt in synchronized....
Similar Threads
"this " & "super"
Thread question
Regarding synchronization
instance members, shared?
doubt on constructor