Last active
August 29, 2015 14:02
-
-
Save pjdietz/1f2dd819fdf110ad4a5a to your computer and use it in GitHub Desktop.
Java class that performs an operation after a predetermined number of actions occur.
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
import java.util.concurrent.atomic.AtomicInteger; | |
/** | |
* Performs an operation after a predetermined number of actions occur. | |
*/ | |
abstract public class CountDown { | |
private AtomicInteger count; | |
/** | |
* Create a new instance that will fire its onReady after count calls to countDown() | |
* @param count The number of countDown() call required | |
*/ | |
public CountDown(int count) { | |
this.count = new AtomicInteger(count); | |
} | |
/** | |
* @return Number of remaining calls to countDown() required | |
*/ | |
public int get() { | |
return count.get(); | |
} | |
/** | |
* Reduce the count of the internal counter by one and call onReady if the counter reaches zero. | |
*/ | |
public void countDown() { | |
int current = count.decrementAndGet(); | |
if (current == 0) { | |
onReady(); | |
} | |
} | |
/** | |
* Method to call when the internal counter reaches zero. | |
*/ | |
abstract protected void onReady(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment