Java Reference
In-Depth Information
public class Waiter {
// . . .
private Thread thread;
private volatile boolean flag;
private final Object lock = new Object();
public boolean stop() {
if (thread != null) {
synchronized (lock) {
flag = true;
lock.notifyAll();
}
return true;
}
return false;
}
}
Applicability
Relyingonthe
Thread
class's
sleep()
,
yield()
,and
getState()
methodsforsynchron-
ization control can cause unexpected behavior.
Bibliography
[Goetz 2006]
[JLS 2013]
§17.3, “Sleep and Yield”
68. Do not assume that the remainder operator always returns a
nonnegative result for integral operands
The JLS, §15.17.3, “Remainder Operator
%
” [JLS 2013], states,
The remainder operation for operands that are integers after binary numeric promo-
tion (§5.6.2) produces a result value such that
(a/b)*b+(a%b)
is equal to
a
. This
identity holds even in the special case that the dividend is the negative integer of
largest possible magnitude for its type and the divisor is
-1
(the remainder is
0
).
It follows from this rule that the result of the remainder operation can be negative
only if the dividend is negative, and can be positive only if the dividend is positive;