Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5927835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:13:29+00:00 2026-05-22T14:13:29+00:00

I am preparing for interviews and just want to prepare some basic threading examples

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:13:30+00:00Added an answer on May 22, 2026 at 2:13 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am preparing for an interview and I came across these questions. Can some
Just preparing myself for a task that i feel could be quite troublesome, and
I'm preparing for interviews..I wanna do some research before going to one..When I was
I came across a good question while preparing for some programming interviews. Given a
I'm preparing some educational/training material with respect to Unit Testing, and want to double
I am preparing for my interview and came across this question: Consider that i
I'm preparing to deploy my Django app and I noticed that when I change
I am preparing to use continuous integration for the first time. I will be
I'm preparing a class on Visual Basic 2005 targeting Visual Basic 6 programmers migrating
While preparing for the SCJP-6 exam I faced with a difficult issue. I can’t

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.