• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

blocked state

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..


 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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..
     
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    ---------please ignore this post(by mistake instead of editing i quoted my post)------------
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    IntelliJ IDE Clojure Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Saravanan, this is a major problem in thread concurrency. Have a look on this and this
     
    this llama doesn't want your drama, he just wants this tiny ad for his mama
    Free, earth friendly heat - from the CodeRanch trailboss
    https://www.kickstarter.com/projects/paulwheaton/free-heat
    reply
      Bookmark Topic Watch Topic
    • New Topic