File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread behavior with synchronized method

 
Ravi Kotha
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ravi Kotha
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

Now I got a better understanding of Thread. Thank you very much for your reply.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic