Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FrogThread implements Runnable{
- private FrogAction frogAction;
- private int index;
- private int sum;
- public FrogThread(FrogAction frogAction, int index, int sum) {
- this.frogAction = frogAction;
- this.index = index;
- this.sum = sum;
- }
- @Override
- public void run() {
- try {
- frogAction.next(index, sum);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public class FrogAction {
- private short n;
- private short[] comars;
- private TreeSet<Integer> set = new TreeSet<>();
- public FrogAction(short n, short[] comars) {
- this.n = n;
- this.comars = comars;
- }
- public TreeSet<Integer> getSet() {
- return set;
- }
- public void next(int index, int sum) throws InterruptedException {
- if (index == n - 1) {
- sum += comars[index];
- set.add(sum);
- } else if (index < n - 1) {
- sum += comars[index];
- Thread threadLeft = new Thread(new FrogThread(this, index + 2, sum));
- Thread threadRight = new Thread(new FrogThread(this, index + 3, sum));
- threadLeft.start();
- threadRight.start();
- threadLeft.join();
- threadRight.join();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement