Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. interface OnEndCallback {
  4.     public void onEnd(final int output);
  5. }
  6.  
  7. class R implements Runnable {
  8.     private final OnEndCallback cb;
  9.     private final int input;
  10.  
  11.     public R(final int input, final OnEndCallback cb) {
  12.     this.input = input;
  13.     this.cb = cb;
  14.     }
  15.  
  16.     public void run() {
  17.     System.out.println("" + Thread.currentThread() + ": input=" + input);
  18.     try {
  19.         Thread.sleep((new Random()).nextInt(3000));
  20.     } catch (InterruptedException unused) {
  21.     }
  22.     cb.onEnd(input + 1);
  23.     }
  24. }
  25.  
  26. class Sync implements OnEndCallback {
  27.     public static void main(final String[] args) {
  28.     final Sync s = new Sync();
  29.     s.go(0);
  30.     }
  31.  
  32.     void go(final int input) {
  33.     if (input < 5) {
  34.         new Thread(new R(input, this)).start();
  35.     }
  36.     }
  37.  
  38.     public void onEnd(final int output) {
  39.     go(output);
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement