Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- interface OnEndCallback {
- public void onEnd(final int output);
- }
- class R implements Runnable {
- private final OnEndCallback cb;
- private final int input;
- public R(final int input, final OnEndCallback cb) {
- this.input = input;
- this.cb = cb;
- }
- public void run() {
- System.out.println("" + Thread.currentThread() + ": input=" + input);
- try {
- Thread.sleep((new Random()).nextInt(3000));
- } catch (InterruptedException unused) {
- }
- cb.onEnd(input + 1);
- }
- }
- class Sync implements OnEndCallback {
- public static void main(final String[] args) {
- final Sync s = new Sync();
- s.go(0);
- }
- void go(final int input) {
- if (input < 5) {
- new Thread(new R(input, this)).start();
- }
- }
- public void onEnd(final int output) {
- go(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement