• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread behavior with synchronized method

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

Now I got a better understanding of Thread. Thank you very much for your reply.
reply
    Bookmark Topic Watch Topic
  • New Topic