• 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 Synchronization

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading some articles abt threads. I came across the statement,


When a class with synchronized method is instantiated, new object is given monitor. That is, once a thread enters a monitor all other threads must wait until the first exits.


Now suppose i have a class that extends Thread and if i have synchronized method inside it, then does that mean that until this thread exits, no other thread can execute that access the same object as the first thread?
I know that if u have a synchronized section(critical) in a code (eg. synchronized (obj){...}), then the object specified gets locked. But what abt synchronized method? does it lock any resource? I am finding it very confusing.....what is the exact use and difference of having a method synchronized and having a block of code synchronized ?
[This message has been edited by Rashmi Gunjotikar (edited August 07, 2001).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, a synchronized method locks on the object of the class, in case of a static synchronized method, it is the class itself (very important).
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A thread executing a synchronized section (synchronized(someobj)) acquires a lock that is part of someobj and releases it when the critical section has completed. Other threads that wish to execute that same critical section on that same object must acquire that lock before they continue so they will block (become part of the waitset of someobj) until the first thread has completed.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If one thread invokes a synchronized method on an object, the lock of that object is first acquired, the method body executed, and the lock released.
Any thread invoking a synchronized method on that object will block.
Unsynchronized access does not wait for any locks but proceeds regardless of locks that may be held on the object.
reply
    Bookmark Topic Watch Topic
  • New Topic