Created
December 27, 2020 21:22
-
-
Save reime005/46f081e1abc1ac39b33c5ecfbdce4f88 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
interface ISomeEventListener { | |
void someEvent(); | |
} | |
class SynchronousExample { | |
private ISomeEventListener someEventListener; | |
public void setSomeEventListener(ISomeEventListener someEventListener) { | |
this.someEventListener = someEventListener; | |
} | |
public void doSomeHeavyCalculation(final long howLongInMs) { | |
System.out.println("Doing some heavy work..."); | |
// some heavy work... | |
try { | |
Thread.sleep(howLongInMs); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
if (this.someEventListener != null) { | |
someEventListener.someEvent(); | |
} | |
} | |
public static void main(String[] args) { | |
final SynchronousExample example = new SynchronousExample(); | |
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