Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@reime005
Created December 27, 2020 21:23
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 reime005/d138a5f92b41bb5de25966534e2b3d9a to your computer and use it in GitHub Desktop.
Save reime005/d138a5f92b41bb5de25966534e2b3d9a to your computer and use it in GitHub Desktop.
class AsynchronousExample {
private ISomeEventListener someEventListener;
public void setSomeEventListener(ISomeEventListener someEventListener) {
this.someEventListener = someEventListener;
}
public void doSomeHeavyCalculation(final long howLongInMs) {
// some heavy work...
new Thread(() -> {
try {
System.out.println("Doing some heavy work...");
Thread.sleep(howLongInMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (this.someEventListener != null) {
someEventListener.someEvent();
}
}).start();
}
public static void main(String[] args) {
final AsynchronousExample example = new AsynchronousExample();
example.setSomeEventListener(() -> {
System.out.println("Received some event!");
});
example.doSomeHeavyCalculation(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment