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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:03:24+00:00 2026-06-08T05:03:24+00:00

Suppose I have a Master that keeps a list of SlaveThread objects. On each

  • 0

Suppose I have a Master that keeps a list of SlaveThread objects. On each time step, I want the Master to run the SlaveThreads in parallel, however, at the end of the time step, I want the SlaveThreads to to wait for each other to complete the current time step before advancing forward. Also, I don’t want to reinstantiate the SlaveThreads on each time step. I have 2 possible solutions, and I don’t know how to make either of them work:

1) The run() method in SlaveThread is in a while(true) loop. After the execution of a single loop in the SlaveThread, I’ll have SlaveThread notify the Master(which I don’t know how to do), and the Master does something like

try{
    for (int i = 0; i < numSlaveThreads; i++) {
        while (!slaveThreads[i].getCompletedThisIter()) {
        wait()
        }
      }
  System.out.println("Joined");

}

before advancing to the next time step. How would I do this? How can I have a single SlaveThread notify just the master?

2) The run() in Slave is not in while(true) loop, then I have to call start() on it on every iteration. But the thread state of the Slave at this point will be terminated. How can I call start() on it again without reinstantiating it?

  • 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-08T05:03:26+00:00Added an answer on June 8, 2026 at 5:03 am

    That is exactly what barriers are for, you can realize this with a CyclicBarrier or a CountDownLatch. These are synchronizers used to delay the progress of threads until the desired state is reached, in your case the threads have completed their computation.

    Here it depends on the details how you want to realize:

    Latches are for waiting for events; barriers are for waiting for other
    threads.

    For the CyclicBarrier that would done in the following fashion:

    // whereby count is the number of your slave threads
    this.barrier = new CyclicBarrier(count); 
    

    Then in the Runnable definition of your slaves you will insert at the end of the computation: barrier.await()

    public class Slaves implements Runnable {
    
       // ...
    
       @Override
       public void run() {
    
          while(condition) {
    
             // computation
             // ...
    
             try {
                // do not proceed, until all [count] threads
                // have reached this position
                barrier.await();
             } catch (InterruptedException ex) {
                return;
             } catch (BrokenBarrierException ex) {
                return;
             }
          }
       }
    }
    

    Your slave threads will not proceed, until all your threads have finished the computation. This way you do not need to realize signaling between another master thread.

    If, however, you have some code you want to execute after all threads have reached that position (master signaling) you can pass an additional Runnable to the CyclicBarrier constructor, which will be executed after all threads have arrived at the barrier.

    this.barrier = new CyclicBarrier(count,
       new Runnable() {
          @Override
          public void run() {
             // signal your master thread, update values, etc.
          }
        }
     );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose i have one dowpdown in master page and i want that when user
Suppose I have a Site.Master page. I want to display something like the version
Suppose you have 2 Properties objects. One contains master properties, the other one is
Suppose the following scenario: I have a master database that contains lots of data,
Suppose I have a large list of words. For an example: >>> with open('/usr/share/dict/words')
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose I have a class Baz that inherits from classes Foo and Bar ,
Suppose I have a process that is updating a record and encounters a record
Suppose I have two h:inputText components. I want to bind both of the text

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.