Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

thread

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible to synchronized a synchronize thread?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't sync on a Thread, you sync on an object.
So once an object has been entered that is synchronized then no other thread can access that object until released...
David.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Short answer - yes. But is it required?? it depends on what you are trying to synchronize.

Synchronization is internally implemented using locks( aka monitors ). When you enter a synchronized block, the thread obtains the lock and retains it till the duration of the method call. So, if you try to double-synchronize, ie., call another synchronized method from originally synchronized method, it will have no effect since the thread already has a lock. This approach ( synch-method1 calling synch-method2 ) is not recommended because it could lead to potential deadlocks.
Synchronization can also be done on different objects instead of a method. If you use the <code>synchronized(Object)</code> format, then the thread obtains the lock on the object instead of 'this' instance. Within the synchronized code, the thread can again obtain a lock on another object. Java imposes no limit on howmany objects a thread can obtain a lock on, but again, do this with caution!!
Lastly, since wording can be so confusing, I just want to make one point clear - you don't synchronize a thread, you synchronize an object( ie., access to an object ).

Hope that helps,
Ajith
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'chaitanyakumar'
Your name does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic