This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread behavior with synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread behavior with synchronized method" Watch "Thread behavior with synchronized method" New topic
Author

Thread behavior with synchronized method

Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Frnds,

I have a small confusion on Thread synchronization. When you have synchronized method, basically it acquires the lock for the object and releases it when it is done. Today I ran a sample test program for threads. I have a class named Test with two methods(one synchronized and one non-synchronized method). In my program I am creating two threads and in the run method I am calling those two methods of Test class. For better understanding here I copied my program



When i run the above program I get this output:

Synchronized method T2
Synchronized method T2
Synchronized method T2
Synchronized method T2
Synchronized method T1
When Thread T2 has acquired the lock for Test object and working on how Thread T1 can acquire the lock??
Synchronized method T2
Synchronized method T1
Synchronized method T2
Synchronized method T2
Synchronized method T2
Synchronized method T2
Synchronized method T2
@@@@@@Non-Synchronized method T2
Synchronized method T1
@@@@@@Non-Synchronized method T2
Synchronized method T1
@@@@@@Non-Synchronized method T2
Synchronized method T1
@@@@@@Non-Synchronized method T2
Synchronized method T1
@@@@@@Non-Synchronized method T2
Synchronized method T1
@@@@@@Non-Synchronized method T2
Synchronized method T1
@@@@@@Non-Synchronized method T2
@@@@@@Non-Synchronized method T2
@@@@@@Non-Synchronized method T2
@@@@@@Non-Synchronized method T2
Synchronized method T1
Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1
@@@@@@Non-Synchronized method T1

So, in the above output while one thread is processing synchronized method of Test object the other thread has got the lock for the same method? Why is that? If I have two synchronized methods then the other thread waits to get the lock till one thread is finished. Guyz shouldnt I use non-synchronized method in an object with synchronized method?

Thanks,
Ravi

[ January 29, 2008: Message edited by: Ravi Kotha ]
[ January 29, 2008: Message edited by: Ravi Kotha ]
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Hi Ravi,

You are correct that using the synchronized keyword does lock an object so another thread cannot enter its synchronized code until the thread that currently has the lock exits that block of code. However, one important fact is that the synchronized keyword synchronizes on the "this" keyword. So the following are essentially the same (there are slight differences in the sense that synchronization in the second method occurs once you get right inside the method):


In your example, each thread is initialized with a new instance of TestRSSI. So each one is actually synchronizing on a different instance of "this". You can try printing the hash code of the object and you'll see the two objects have different hash codes.

The following example does in fact synchronize:


Note that here both threads use the same instance of synchClass and thus synchronize on the same lock. Hope that helps.

Jeff
[ January 29, 2008: Message edited by: Jeff Storey ]

Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Jeff,

Now I got a better understanding of Thread. Thank you very much for your reply.
 
Don't get me started about those stupid light bulbs.
 
subject: Thread behavior with synchronized method
 
Similar Threads
Doubt in threads
deadlock?
Explain output of this program
help regarding synchronized methods
Thread Synchronization Q