2

Is there any way I could control the order in which threads should resume its work after cyclic barrier "Barrier Action" has been completed?

Following is an example which I tried but order of final 2 statements keep changing but I don't want that to happen.It should always be:-

Thread 2 has crossed the barrier
Thread 1 has crossed the barrier

Code:-

public class CyclicBarrierExample {

    private static class Task1 implements Runnable {

        private CyclicBarrier barrier;

        public Task1(CyclicBarrier barrier) {
            this.barrier = barrier;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is waiting on barrier");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " has crossed the barrier");
            } catch (InterruptedException | BrokenBarrierException ex) {
                System.out.println("Exception occured");
            }
        }
    }

    private static class Task2 implements Runnable {

        private CyclicBarrier barrier;

        public Task2(CyclicBarrier barrier) {
            this.barrier = barrier;
        }

        @Override
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is waiting on barrier");
                barrier.await();
                System.out.println(Thread.currentThread().getName() + " has crossed the barrier");
            } catch (InterruptedException | BrokenBarrierException ex) {
                System.out.println("Exception occured");
            }
        }
    }

    public static void main(String args[]) throws InterruptedException {

        final CyclicBarrier cb = new CyclicBarrier(2, ()->{
                //This task will be executed once all thread reaches barrier
                System.out.println("All parties are arrived at barrier, lets play");
        });

        //starting each of thread
        Thread t1 = new Thread(new Task1(cb), "Thread 1");
        Thread t2 = new Thread(new Task2(cb), "Thread 2");

        t1.start();
        t2.start();

    }
}

Output:-

Thread 1 is waiting on barrier
Thread 2 is waiting on barrier
All parties are arrived at barrier, lets play
Thread 2 has crossed the barrier
Thread 1 has crossed the barrier
Jaspreet Jolly
  • 1,235
  • 11
  • 26
  • what do you want the order to be? You say, thread 2 should always be first, but why? – matt Jul 11 '19 at 05:11
  • That would defeat the purpose of having multiple threads if you want to do things sequentially. If you really want to do that, one way is to do `Thread.sleep(10)` in `Task1`. – Kartik Jul 11 '19 at 05:13
  • Or you can use a queue and read from it when all threads reach the barrier. – Kartik Jul 11 '19 at 05:21
  • @Kartik no it would not defeat the purpose of multithreading. Suppose in a online card game I want to wait for min 5 participants to start the game(5 threads), I used cyclic barrier and when all 5 participants called await, game starts but I want the order in which participants should play cards be same in which they joined.Person who joined first will start. – Jaspreet Jolly Jul 11 '19 at 09:21
  • That doesn't sound like something that needs multiple threads. – matt Jul 12 '19 at 05:39

0 Answers0