File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Transistion from blocked to runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Transistion from blocked to runnable" Watch "Transistion from blocked to runnable" New topic
Author

Transistion from blocked to runnable

Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
HI,
When thread1 tries to access a synchronized method which has been locked by another thread2, then thread1 goes to blocked state from running state. I want to understand in detail how does thread2 goes from blocked to runnable state?

Does this transition happen ONLY once thread2 releases the lock? If yes, then how thread1 comes to know that thread2 has released the lock (Consider this just a simple scenario where we don't have wait & notify mechanism)?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
At entry to a synchronized block of code, a thread will be blocked if the requested
object lock is not available. It becomes runnable again when the JVM grants it the
lock. When the thread will actually run again is another JVM decision. That's it.

Jim ... ...



BEE MBA PMP SCJP-6
Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
Got it Jim... Just one more small query, its JVM who will grant the lock or the thread scheduler.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Good question. I don't really now if the scheduler is considered part of the JVM or not.

Jim ... ...
Rajesh Nagaraju
Ranch Hand

Joined: Nov 27, 2003
Posts: 60
Thread scheduler is only going to decide which thread has to get the opportunity to run and will come into effect after the thread has been in the runnable state. So def. not the scheduler is what I feel..
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Answers to your questions lie here.

You would also like to read the java language specifications


apigee, a better way to API!
getshijos joseph
Greenhorn

Joined: May 28, 2010
Posts: 5
Every object has a waitset associated with it. When a thread come to aquire the lock on a synchronised block , if the lock is unavailable(ie some other thread is executing the block) , it will be placed in the waitset. When the active thread finishes execution , it releases the lock and thread scheduler will pick a thread from the wait set (according to priority . which one will get its turn , is not in programmers scope if all are of same priority) , and that thread becomes runnable and will aquire the lock.

Hope this helps


admin
www.javarecharge.com
||||||||||||||................
Wanna recharge
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Threads block because they are waiting on object locks. They are not waiting
for access to synchronized code. A different thread can freely use the same
synchronized code to operate on a different object. It is when the requested
object lock is granted that a blocked thread once more becomes runnable.

Jim ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transistion from blocked to runnable