Problems of Coordinated methods using join with CyclicBarrier

58 views Asked by At

I tried to learn more of using join() with CyclicBarrier in a coordinated method scenario - Send and Receive. The basic codes were from previous poster and I modified it to run certain loop account (ex 10 times). The codes as:

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.BrokenBarrierException;

public class Test {

    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(2);
        Receiver receiver = new Receiver(barrier);
        Sender sender = new Sender(barrier);

        Thread r = new Thread(receiver);
        Thread s = new Thread(sender);  
        r.start();
        s.start();
        try {
            r.join();
            s.join();       
        } catch (InterruptedException e) {
             e.printStackTrace();
        }
        System.out.println("It is over....");
    }
}

class Sender implements Runnable {

    private CyclicBarrier barrier;
    static boolean stop = false;
    private static int i = 1;
    public static int limit = 10 ;

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

    @Override
    public void run() {
        while ( !stop ) {
            try {
                // Wait for notify.
                Thread.sleep(1);
                // Now do SEND.
                int j = getI();
                System.out.println(j);  
                System.out.println("SEND -" + j );
                barrier.await();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public static int getI() {
        return i;
    }

    public static void setI(int i) {
        Sender.i = i;
    }
}

class Receiver implements Runnable {

    private CyclicBarrier barrier;    

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

    @Override
    public void run() {
        while ( Sender.getI() <= Sender.limit ) {   
            // Then notify.
            try {
                barrier.await();
                int k = Sender.getI();                              
                // Wait for ACK (the sleep just simulates that).
                System.out.println("ACK -" + k );
                Sender.setI(++k);
                if ( Sender.getI() > Sender.limit ) {
                    System.out.println(k);
                    Sender.stop = true;
       //           barrier.reset();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}

However, I run into a result that I could not resolve and is little confused with the logic of await(). Although the result seems as my expected, i.e. SEND and ACK in alternatively 10 times. It actually ran extra time on SEND and apparently the change of the status Sender.stop to true state at the 10th -ACK run did not 'stop' the while loop in the Send-run(). In addition, the Thread did not end after the 10 times (or 11 times), therefore I never got into the "It is over.." print out in main(). It seemed that the process was hung somewhere.

0

There are 0 answers