Skip to content

Instantly share code, notes, and snippets.

@rgb-24bit
Created February 22, 2021 12:40
Show Gist options
  • Select an option

  • Save rgb-24bit/15440e187942be8574f56a782086e06e to your computer and use it in GitHub Desktop.

Select an option

Save rgb-24bit/15440e187942be8574f56a782086e06e to your computer and use it in GitHub Desktop.
Only do once.
package sync;
public class Once {
private boolean done;
public void run(Runnable runnable) {
if (!done) {
slowRun(runnable);
}
}
private synchronized void slowRun(Runnable runnable) {
if (!done) {
try {
runnable.run();
} finally {
done = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment