Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic barrier Java, How to verify?

I am preparing for interviews and just want to prepare some basic threading examples and structures so that I can use them during my white board coding if I have to.

I was reading about CyclicBarrier and was just trying my hands at it, so I wrote a very simple code:

import java.util.concurrent.CyclicBarrier;

public class Threads
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // ******************************************************************
        // Using CyclicBarrier to make all threads wait at a point until all
        // threads reach there
        // ******************************************************************
        barrier = new CyclicBarrier(N);

        for (int i = 0; i < N; ++i)
        {
            new Thread(new CyclicBarrierWorker()).start();    
        }
        // ******************************************************************
    }

    static class CyclicBarrierWorker implements Runnable
    {
        public void run()
        {
          try
        {
            long id = Thread.currentThread().getId();
            System.out.println("I am thread " + id + " and I am waiting for my friends to arrive");

            // Do Something in the Thread
            Thread.sleep(1000*(int)(4*Math.random()*10));


            // Now Wait till all the thread reaches this point
            barrier.await();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        //Now do whatever else after all threads are released
        long id1 = Thread.currentThread().getId();
        System.out.println("Thread:"+id1+" We all got released ..hurray!!");
            System.out.println("We all got released ..hurray!!");
        }
    }

    final static int     N       = 4;
    static CyclicBarrier barrier = null;
}

You can copy paste it as is and run in your compiler.

What I want to verify is that indeed all threads wait at this point in code:

barrier.await();

I put some wait and was hoping that I would see 4 statements appear one after other in a sequential fashion on the console, followed by 'outburst' of "released..hurray" statement. But I am seeing outburst of all the statements together no matter what I select as the sleep.

Am I missing something here ?

Thanks P.S: Is there an online editor like http://codepad.org/F01xIhLl where I can just put Java code and hit a button to run a throw away code ? . I found some which require some configuration before I can run any code.

like image 919
codeObserver Avatar asked May 21 '11 21:05

codeObserver


1 Answers

The code looks fine, but it might be more enlightening to write to System.out before the sleep. Consider this in run():

        long id = Thread.currentThread().getId();
        System.out.println("I am thread " + id + " and I am waiting for my friends to arrive");
        // Do Something in the Thread
        Thread.sleep(1000*8);

On my machine, I still see a burst, but it is clear that the threads are blocked on the barrier.

like image 121
Michael Easter Avatar answered Sep 21 '22 15:09

Michael Easter