Created
December 27, 2020 21:23
-
-
Save reime005/d138a5f92b41bb5de25966534e2b3d9a to your computer and use it in GitHub Desktop.
This file contains 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
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