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

  • SEARCH
  • Home
  • 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 8618175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:02:36+00:00 2026-06-12T06:02:36+00:00

I have read Bloch’s Item 69(Effective Java) and there was an example of Simple

  • 0

I have read Bloch’s Item 69(Effective Java) and there was an example of Simple framework for timing execution which uses 3 CountDownLatch for synchronization threads. Also Bloch says that this example can be rewritten using 1 CyclicBarrier. I have tried to do this and get the next code:

     public static long time(ExecutorService exec, int count) throws InterruptedException, BrokenBarrierException {
            CyclicBarrier cyclicBarrier = new CyclicBarrier(count+1);
            for (int i = 0; i < count; i++) {
                exec.submit(new Worker(cyclicBarrier, i));
            }
            System.out.println("Start processing");
            cyclicBarrier.await();
            long time = System.nanoTime();
            cyclicBarrier.await();
            long elapsedTime = (System.nanoTime() - time)/1000;
            System.out.println("FIN. ");
            return elapsedTime;

        }
private static class Worker implements Runnable {

        final CyclicBarrier cyclicBarrier;
        int workerNumber;

        Worker(CyclicBarrier cyclicBarrier, int workerCount) {
            this.cyclicBarrier = cyclicBarrier;
            this.workerNumber = workerCount;
        }

        public void run() {
            try {
                cyclicBarrier.await();
                work();
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
        private void work() {
            System.out.println("Worker " + workerNumber + " starts his job");
            System.out.println("Worker " + workerNumber + " finishes his job");
        }

    }

But I think there is a problem here(in time method): after first await I try to measure time of start of threads’ execution, so I want to measure this time BEFORE all another threads starts. There is no any guarantee that this(long time = System.nanoTime();) instruction will execute before another threads start.

How can I achieve such functionality with CyclicBarrier? Any suggestions?

  • 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-06-12T06:02:38+00:00Added an answer on June 12, 2026 at 6:02 am

    CyclicBarrier allows you to define a barrier action to be executed after all threads reached the barrier but before they resumed execution. Use CyclicBarrier(int, Runnable) to define it.

    Obviously, in your case it’s up to you to distinguish between executions of barrier action before doing work and after finishing it, something like this:

    class TimingAction implements Runnable {
        private boolean running;
        private long start;
        private long elapsed;
    
        public void run() {
            if (running) {
                elapsed = System.nanoTime() - start;
                running = false;
            } else {
                start = System.nanoTime();
                running = true;
            }
        }
        ...
    }
    
    TimingAction timer = new TimingAction();
    CyclicBarrier cyclicBarrier = new CyclicBarrier(count + 1, timer);
    ...
    long elapsed = timer.getElapsed();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to develop a simple parser, to read block of text for example:
I read Effective Java by Joshua Bloch and removed the 'Constant Interface anti-pattern' from
After I read Bloch's discussion http://www.infoq.com/news/2010/04/bloch_java_future I started to think about Do I have
I have read this question , but I couldn't create a full example: class
I have read that there is some overhead to using C++ exceptions for exception
I have an application which uses two methods of an API. Both these methods
So I have read this article C# Overloads which says that you can use
I have to read a binary file in a legacy format with Java. In
I have read it here : CodeProject and some other places that there should
I have a static initialization block where I read data from DB into certain

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.