-
Notifications
You must be signed in to change notification settings - Fork 34
Open
Description
public long next() {
// This needs to be inside the synchronized block:
long currentTime = System.currentTimeMillis();Without that change, two threads (call them thread-a and thread-b) call next();
- Thread-a gets time t
- Thread-b gets time t+1
- Thread-a is interrupted for some reason (due to cpu scheduling, for example)
- Thread-b enters the synchronized block; and sets referenceTime to to time t+1.
- Thread-a blocks on synchronized block.
- Thread-b exits the block, relinquishing the lock.
- Thread-a enters the synchronized block, and compares it's currentTime (t) with the referenceTime (t+1), and throws a RuntimeException.
fit2cloud-zaopengguo, GuiSim, cw760, newpasung and hfy0
Metadata
Metadata
Assignees
Labels
No labels