Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread synchronization problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, this is an exampl from SCJP 6 book (Sierra, Bates) about threads. But i dont works as it should i I dont know why... Can somebody help?

If I understand it well, when I call notify() method, one thread which waits for this objects lock move from blocked state do runnable. And when I call notifyAll() all threads move in the same way.

But then following code dont work as it should:





So the result should be only one Thread ends. But there all always more outputs. So three threads Reader were created and each take a lock and sleep 100 milis, but meanwhile thread Calculator starts and notify one thread. It is possible that when it calls notify(), just the first thread should sleep so no output sould be done. But what is strange, is that notifies 1, 2 or 3 threads.
Another strange thing is that when i comment CENTRAL POINT row, so Calculator dont notify anybody, program always make output. Shouldn't it just stop, because of waiting threads? The same thing if i remove synchronized block from Calculator.
 
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This issue seems to come up often... short answer is.... Don't use the Thread object for wait() and notify().

The core libraries use it to implement the join() method -- when a thread finishes, a notifyAll() is sent (using the Thread object) to wake up all threads that are waiting to join.

Henry
 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The core libraries use it to implement the join() method -- when a thread finishes, a notifyAll() is sent (using the Thread object) to wake up all threads that are waiting to join.
Hi Henry, I guess notify() or notifyAll() for waking up Threads are used only with wait().(not with join())
 
Henry Wong
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:Hi Henry, I guess notify() or notifyAll() for waking up Threads are used only with wait().(not with join())



Please read my post again. I never said notifyAll() is supposed to be called to wake up threads that call join(). I said that there is a call to notifyAll() that is called automatically, as part of the thread shutdown process, to wake up threads that call the join() method. The join() method is implemented using a loop that checks the alive flag, and call the wait() method.

Henry
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic