Created
February 8, 2020 04:11
-
-
Save andriybuday/79dfb21e1d57065bf144fe1e6ccecb8c to your computer and use it in GitHub Desktop.
RunnableWithSyncExample
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
public class RunnableWithSyncExample implements Runnable { | |
private Object someSharedObject; | |
public SomethingRunnableWithSync(Object sharedObject) { | |
someSharedObject = sharedObject; | |
} | |
public void run() { | |
synchronized(someSharedObject) { | |
// do something with someSharedObject | |
// and then optionally wait or notify/notifyAll | |
someSharedObject.wait(); | |
// or | |
someSharedObject.notifyAll(); | |
} | |
} | |
} | |
// sample usage | |
(new Thread(new RunnableWithSyncExample())).start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment