• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

inter thread communication notify is not getting called

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have created a class called hotel  in which  i have set a boolean variable called conditionVar to false. inside the run method i have used two synchonized blocks, one is for wait and other is for notify .But the application is showing output as




it is not entering in the synchronized block of notify . Is this an concept of busy waiting? and how can the thread call notify?





 
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The block starting at line 28 can never be entered by either thread.

Why? Because the block starting at line 11 is an infinite loop. Control cannot exit from that block until the conditionVar variable is set to true, which cannot happen.

Still not persuaded? Consider thread A. It can't get out of the line-11 block until some other thread sets conditionVar to true. Now consider thread B. It can't get out of the line-11 block until some other thread sets conditionVar to true. So B would have to set it to allow A to proceed, and A would have to set it for B to proceed. I suppose you could call that self-inflicted deadlock.
 
amithk kumar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so how to make the thread call the notify?
 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amithk kumar wrote:so how to make the thread call the notify?


Can you explain which thread you want to call notify and what state it needs detect to know that it  should call notify?

How are the two threads supposed to interact?  Why the use of wait and notify?
 
Paul Clapham
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you remove lines 13 and 23 from your code then it will call wait and then go on directly to call notify. That satisfies your stated goal of calling notify, but like Norman I have no idea of why you would want it to do that. I don't understand what the code is supposed to be doing.
 
Paul Clapham
Sheriff
Posts: 28330
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you remove lines 13 and 23 from your code then it will call wait and then go on directly to call notify.



No, actually it won't. You still have the deadlock where both threads start out by waiting. How you "fix" that I have no idea, because that would require knowing what the code was really meant to do.
 
Bartender
Posts: 5567
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the current situation, only an external factor can break the deadlock. For instance:

 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic