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 6087133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:50:09+00:00 2026-05-23T11:50:09+00:00

I got a bunch of threads that perform calculations. They are synchronized using a

  • 0

I got a bunch of threads that perform calculations. They are “synchronized” using a CyclicBarrier. When any thread’s run() method finishes, I want all other threads to exit as well once they call await() on the barrier the next time.

So far, everything I’ve tried either hangs at the await() calls or results in a broken barrier. Any tips?

EDIT: Here’s the (basic) code:

public MyClass implements Runnable {
    public void run() {
        while (true) {
            if (someCondition) {
                // quit other threads when they call await()
                return;
            }
            barrier.await();
    }
}
  • 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-23T11:50:10+00:00Added an answer on May 23, 2026 at 11:50 am

    reset() will awaken all waiting threads with a thrown exception

    you can then use the await as so

    private static volatile boolean shouldStop=false;
    
    public void run() {
        try{
            while (true) {
                if (someCondition) {
                    // quit other threads when they call await()
                    return;
                }
                try{
                    if(shouldStop)return;
                    barrier.await();
                }catch(BrokenBarrierException e){
                    //someone stopped 
                    return;
                }
           }
       }finally{
           shouldStop =true;
           barrier.reset();
       }
    }
    

    you can also call a method for the if(shouldStop) check

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

Sidebar

Related Questions

I got a bunch of constants that I use for my tests. They look
If I've got a bunch of image resources that I made using the GD
I got a thread dump of one of my processes. It has a bunch
I've got a bunch of legacy code that I need to write unit tests
Say I've got a bunch of files that are all >100 lines long. I'd
I've got a bunch of first names in a field that carry a middle
I've got a Python script that does a bunch of things in a single
So I've got a bunch of worker threads doing simple curl class, each worker
I've got a bunch of tiles that make up a single image, that are
Is there any way to extract hyperlinks from .doc. I got bunch of hyperlinks

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.