• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread: wait() and notify()

 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying Thread but I don't fully understand yet. I got NullPointerException from the program below.
Please shed a light.




// Below is the program I ran.

}
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
operator is still null because you never give it a value.
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:operator is still null because you never give it a value.


It probably is a dumb question.
I add a line: Operator operator = new Operator() but it waits forever, never come back.

 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's because of the "while (true)". That means never stop. However, even if that loop would end the thread would not. That's because there will never be a notify(All) on that very same Operator instance.

Try the following:
This will print "Waiting", then waits 5 seconds, then will alternatively print out "No more" and "Waiting" until you press CTRL+Z to terminate the JVM.
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Well, that's because of the "while (true)". That means never stop. However, even if that loop would end the thread would not. That's because there will never be a notify(All) on that very same Operator instance.

Try the following:
This will print "Waiting", then waits 5 seconds, then will alternatively print out "No more" and "Waiting" until you press CTRL+Z to terminate the JVM.


Thank you very much !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic