• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Threads (wait & notify)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I came up with this bit of code below to practice wait() and notify(). Class Changeable has a boolean variable, notifies the Observer class from it's run method, which then stops waiting and prints Changeable's state. Observer checks for notifications from a while loop. All normal so far. When I run it, it seems that not all the notifications are received. Sometimes more, sometimes less, but never all.

Output varies, but looks like this:

Changeable running. State is: true 1
Changeable running. State is: true 2
Observer running. Changeable's state is true 1
Changeable running. State is: true 3
Observer running. Changeable's state is true 2
Changeable running. State is: true 4
Observer running. Changeable's state is true 3
Changeable running. State is: true 5
Changeable running. State is: true 6
Observer running. Changeable's state is true 4
Changeable running. State is: true 7
Observer running. Changeable's state is true 5
Changeable running. State is: true 8
Observer running. Changeable's state is true 6
Changeable running. State is: true 9
Observer running. Changeable's state is true 7
Changeable running. State is: true 10
Observer running. Changeable's state is true 8

Here's the code:
 
author
Posts: 23956
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
If a notification is sent, and there isn't a thread waiting for it, it will be lost.

Henry
 
What's that smell? Hey, sniff this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic