IntelliJ Java IDE
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronization Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Synchronization Problem" Watch "Synchronization Problem" New topic
Author

Synchronization Problem

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 323
If a thread(Thread-A) enters in a synchronize block, it acquires lock on synchronized object. As soon as the thread exits the synchronized block this lock is released (please correct me.)

Suppose while Thread-A was executing the synchronize block, 3 other thread tried to acquire the lock on this objected and ended in the wait pool of that object. If our synchronize block do not have obj.notifyAll() / obj.notify() (where obj is our synchronized object) then how waiting threads will be notified after the Thread-A exits synchronize block.

Please help


"Know where to find the solution and how to use it - that's the secret of success."
Parashuram Hallur
Ranch Hand

Joined: Sep 18, 2006
Posts: 41
Well, notify() and notifyAll() applies only to threads which are waiting by calling the wait() method. Otherwise synchronization works in such a way that, the first thread which enters the synchronized method/block will acquire the lock and any subsequent threads trying to execute the same method/block will be kept waiting in the pool so that once the lock on the method or that block is released any thread which is waiting for in the pool can acquire the lock, which thread acquires is not guaranteed, it is scheduler defendant
Mo Jay
Ranch Hand

Joined: Feb 16, 2009
Posts: 83
It looks like there is some sort of periodic check after sometime for the lock by the threads wanting to acquire it, when lock becomes available after the first thread releases it then the scheduler will pick some thread among those threads that made the request for the lock.

Cheers!!
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Amirr Rafique wrote:
Suppose while Thread-A was executing the synchronize block, 3 other thread tried to acquire the lock on this objected and ended in the wait pool of that object. If our synchronize block do not have obj.notifyAll() / obj.notify() (where obj is our synchronized object) then how waiting threads will be notified after the Thread-A exits synchronize block.
Please help

I guess they will never notified, but another thread could interrupts a waiting thread, which then will throw an InterruptedException.
Check the API for this.

cheers
Bob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 14606


Maybe it would be better to use the terminology "blocked on a synch lock", instead of "wait", because threads waiting to acquire a lock has nothing to do with the wait/notify mechanism.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 323
I experienced a scenario in which my threads was supposed to get respective resource from a Hashtable and proceed with their processing. Since Hashtable is thread safe to i did not put any synchronized block around it.
But due to some reason, my threads were getting lost in this particular method. Then I put synchronized block around Hashtable (getting lock on Hashtable Object) and placed a hashtableObj.notifyAll() in it. After this change the problem was fixed and my code is working fine.

I am still not convinced with my solution but due to some reason it is working. Anyone please help me in understanding the problem
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 269
Amirr Rafique wrote:Then I put synchronized block around Hashtable (getting lock on Hashtable Object) and placed a hashtableObj.notifyAll() in it. After this change the problem was fixed and my code is working fine.

Again, if you're not calling hashtableObject.wait() anywhere, you don't need to call hashtableObject.notifyAll().


SCJP 6 || SCWCD 5
 
 
subject: Synchronization Problem
 
Threads others viewed
Threads
synchronized
Difference between synchronizing a method and synchronizing a block of code in the method
synchronization quesion
synchronized
developer file tools