codeObserver
codeObserver

Reputation: 6647

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.

Upvotes: 5

Views: 1339

Answers (3)

Vineet Reynolds
Vineet Reynolds

Reputation: 76709

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.

The behavior I'm observing is that all the threads created, sleep for approximately the same amount of time. Remember that other threads can perform their work in the interim, and will therefore get scheduled; since all threads created sleep for the same amount of time, there is very little difference between the instants of time when the System.out.println calls are invoked.

Edit: The other answer of sleeping of a random amount of time will aid in understanding the concept of a barrier better, for it would guarantee (to some extent) the possibility of multiple threads arriving at the barrier at different instants of time.

Upvotes: 1

ratchet freak
ratchet freak

Reputation: 48206

if you want to avoid the first burst use a random in the sleep

Thread.sleep(1000*(int)(8*Math.rand()));

Upvotes: 2

Michael Easter
Michael Easter

Reputation: 24478

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.

Upvotes: 2

Related Questions