Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. public class FrogThread implements Runnable{
  2.  
  3.     private FrogAction frogAction;
  4.     private int index;
  5.     private int sum;
  6.  
  7.     public FrogThread(FrogAction frogAction, int index, int sum) {
  8.         this.frogAction = frogAction;
  9.         this.index = index;
  10.         this.sum = sum;
  11.     }
  12.  
  13.     @Override
  14.     public void run() {
  15.         try {
  16.             frogAction.next(index, sum);
  17.         } catch (InterruptedException e) {
  18.             e.printStackTrace();
  19.         }
  20.     }
  21. }
  22.  
  23.  
  24. public class FrogAction {
  25.  
  26.     private short n;
  27.     private short[] comars;
  28.     private TreeSet<Integer> set = new TreeSet<>();
  29.  
  30.     public FrogAction(short n, short[] comars) {
  31.         this.n = n;
  32.         this.comars = comars;
  33.     }
  34.  
  35.     public TreeSet<Integer> getSet() {
  36.         return set;
  37.     }
  38.  
  39.     public void next(int index, int sum) throws InterruptedException {
  40.         if (index == n - 1) {
  41.             sum += comars[index];
  42.             set.add(sum);
  43.         } else if (index < n - 1) {
  44.             sum += comars[index];
  45.             Thread threadLeft = new Thread(new FrogThread(this, index + 2, sum));
  46.             Thread threadRight = new Thread(new FrogThread(this, index + 3, sum));
  47.             threadLeft.start();
  48.             threadRight.start();
  49.             threadLeft.join();
  50.             threadRight.join();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement