Created
January 27, 2014 05:24
-
-
Save royguo/8643632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main; | |
import java.util.concurrent.atomic.AtomicReference; | |
public class CASCalculator extends CalculatorBase { | |
private enum Status { | |
NOT_STARTED, | |
STARTED, | |
DISPOSED | |
} | |
private AtomicReference<Status> status = new AtomicReference<Status>(Status.NOT_STARTED); | |
@Override | |
public void start() { | |
if (status.compareAndSet(Status.NOT_STARTED, Status.STARTED)) { | |
startCore(); | |
} | |
// Do something useful, calculator is started now. | |
} | |
@Override | |
public void dispose() { | |
if (status.compareAndSet(Status.STARTED, Status.DISPOSED)) { | |
disposeCore(); | |
// Do something useful, calculator is disposed now. | |
} else { | |
// Calculator is not started yet or has been disposed. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment