Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjdietz/1f2dd819fdf110ad4a5a to your computer and use it in GitHub Desktop.
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.
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