• Post Reply Bookmark Topic Watch Topic
  • New Topic

blocked state  RSS feed

 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when there is more than one thread in blocked state
and runnable thread releases the lock..will all the
blocked threads enter the runnable state?

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading the documentation, what don't you get? Why don't you try it?
 
saravanan ragunathan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading the documentation, what don't you get? Why don't you try it?


sir ,

i studied that when notify() method is invoked any one of thread in waiting state will enter the runnable state(this is arbitrary). at the end of synchonized block
thread holding the lock automatically releases lock on that object..my question is that when thread releases the lock,will all the blocked threads(threads that was tried to enter the synchronized block and blocked because of other thread holding the lock for that object)enter the runnable state or only one to compete with the thread to acquire the lock that have been awakened through notify() method..


 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saravanan ragunathan wrote:
After reading the documentation, what don't you get? Why don't you try it?


sir ,

i studied that when notify() method is invoked any one of thread in waiting state will enter the runnable state(this is arbitrary). at the end of synchonized block
thread holding the lock automatically releases lock on that object..my question is that when thread releases the lock,will all the blocked threads(threads that was tried to enter the synchronized block and blocked because of other thread holding the lock for that object)enter the runnable state or only one to compete with the thread that have been awakened through notify() method..


notifyAll() will notify all the threads and after that all the threads will contest for the monitor lock. The thread that gets the monitor lock will start the work and the rest will be waiting for the monitor lock.

If you take the thread dump and see the state of the threads (although getting this dump at the correct time is somewhat tough),
  • In case of notify() only one thread will get to the RUNNING state and other will be in WAIT state
  • In case of notifyAll() only one thread will get to the RUNNING state and other will be in BLOCKED state
  •  
    saravanan ragunathan
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In case of notifyAll() only one thread will get to the RUNNING state and other will be in BLOCKED state


    sir,

    i could understand your answer,but i still have one doubt that when the thread releases the lock, will all threads in blocked state enter the runnable state or only one..
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If n(no of Threads) is greater than zero and this is a notifyAll action, then all threads are
    removed from (Object)m's wait set, and thus resume. Notice however, that only one of
    them at a time will lock the monitor required during the resumption of wait
     
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ---------please ignore this post(by mistake instead of editing i quoted my post)------------
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    saravanan ragunathan wrote:
    when the thread releases the lock, will all threads in blocked state enter the runnable state or only one..


    I think, my post DOES explain this!
     
    saravanan ragunathan
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Abimaran Kugathasan wrote:
    saravanan ragunathan wrote:
    when the thread releases the lock, will all threads in blocked state enter the runnable state or only one..


    I think, my post DOES explain this!


    sir,
    consider this situation
    runnable(ready=5 threads,running=0 threads)
    wait=0 threads
    blocked=0 threads

    after one thraed getting the lock
    runnable(ready=0 threads,running=1 thread)
    wait=2 threads
    blocked=2 threads

    when i call the notifyall() which wakeups all threads in wait state
    2 threads in waiting state will go to ready state,then what will happen to blocked threads,when they will wakeup...
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Saravanan, this is a major problem in thread concurrency. Have a look on this and this
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!