[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

Synchronization Problem

Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Messages: 280

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
Messages: 40

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
Messages: 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
Messages: 308

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
JavaEnterpriseEditionFaq - TomcatFaq
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10023


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
Messages: 280

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

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

Joined: Jun 17, 2009
Messages: 76

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().
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
JProfiler
Get rid of your performance problems and memory leaks!