Created
February 22, 2021 12:40
-
-
Save rgb-24bit/15440e187942be8574f56a782086e06e to your computer and use it in GitHub Desktop.
Only do once.
This file contains hidden or 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 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