Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transistion from blocked to runnable

 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...

 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it Jim... Just one more small query, its JVM who will grant the lock or the thread scheduler.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. I don't really now if the scheduler is considered part of the JVM or not.

Jim ... ...
 
Rajesh Nagaraju
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answers to your questions lie here.

You would also like to read the java language specifications
 
getshijos joseph
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic