• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Synchronization  RSS feed

 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • 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).]
 
Saad Ahmed
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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).
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • 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.
 
Ganesh Ram
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!