Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't get notify() to work  RSS feed

 
Jared Folino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically the problem is that the last print statement "Code Block 4" does not print. This is because the main thread that calls notify() completes before the second thread (Thread-1) begins. As such when Thread-1 is called it permanently blocked as their is no active thread to call notify().

 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because the main thread that calls notify() completes before the second thread (Thread-1) begins.

How true.

You need to communicate between the threads, back and forth. Read the tutorial and follow the instructions.
Concurrency
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See how this variation is different:

I narrowed the scope of the synchronized blocks so they only control the wait & notify. Work through what was synchronized in your version and see if that explains your results.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!