This is not my preferred way of doing this, causing massive CPU consumption.
If this is really your working code, just save it like that. Checking the logical value once per second causes a lack of measurable CPU load. Nothing.
The real problem is that the thread that checks the value may not see the changes that have occurred over an arbitrarily long time due to caching. To ensure that the value is always synchronized between threads, you need to put the volatile keyword in the variable definition, i.e.
private volatile boolean value;
Note that including access in a synchronized block, for example, when using the notification solution described in other answers, will have the same effect.
Michael Borgwardt Sep 26 '13 at 10:23 2013-09-26 10:23
source share