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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:59:53+00:00 2026-05-27T07:59:53+00:00

Is there a way to wait all threads in executor pool when pause button

  • 0

Is there a way to wait all threads in executor pool when pause button pressed and rerun play button pressed? I tried CountDownLatch but I dont know I must put it after executor declaration or in run () method? I dont have much information about threads.please can someone tell me how I can do.Thanks

 public static CountDownLatch waiter;
public static ExecutorService pool;

public Action() throws InterruptedException{
    pool=Executors.newFixedThreadPool(2);
    waiter=new CountDownLatch(2); // to wait
    robot1=new Robot(0,560,"rbt1"); // starts random free position
    robot2=new Robot(0,560,"rbt2");
    if(Frame.pause==false){
       pool.submit(robot1);
       pool.submit(robot2);}
    if(Frame.pause==true){
        waiter.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-27T07:59:54+00:00Added an answer on May 27, 2026 at 7:59 am

    Your Robot worker needs a shared thread-safe way to check whether workers should be paused or playing. In the run() method of your worker, if the thread is paused, wait for notification on this lock object. While looping, or whatever it is the worker does, periodically check the state of the lock, and pause the worker if needed.

    pauseIfNeeded() {
        synchronized(workerLock) {
            if (workerLock.isPaused()) {
                workerLock.wait();
            }
        }
    }
    

    Your pause and play button should get a synchronized lock on the workerLock, and set the paused property, and call notify() on the workerLock. This will let the workers pause or continue as needed. The Executor is always “running”, regardless of the paused/playing state.

    EDIT
    You can refactor the above code into its own class, as follows:

    public class WorkerPauseManager {
    
        private boolean paused;
    
        public synchronized void pauseIfNeeded() throws InterruptedException {
            if (paused) wait();
        }
    
        public synchronized void pause() {
            this.paused = true;
        }
    
        public synchronized void start() {
            this.paused = false;
            notifyAll();
        }
    }
    

    Create a single instance of WorkerPauseManager. Pass this instance to all your Robot workers, and keep a reference for the swing pause/play actions to reference. Your worker thread should call pauseIfNeeded.

    Here’s an SCCE using the WorkerPauseManager:

    public class WorkerPauseManagerTest {
        public static void main(String[] args) {
            final WorkerPauseManager pauseManager = new WorkerPauseManager();
            new Worker("Worker 1", pauseManager).start();
            new Worker("Worker 2", pauseManager).start();
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JToggleButton playPauseButton = new JToggleButton(new AbstractAction("Pause") {
                        public void actionPerformed(final ActionEvent e) {
                            JToggleButton source = (JToggleButton) e.getSource();
                            if (source.isSelected()) {
                                pauseManager.start();
                                source.setText("Pause");
                            } else {
                                pauseManager.pause();
                                source.setText("Play");
                            }
                        }
                    });
                    JOptionPane.showMessageDialog(null, playPauseButton, "WorkerPauseManager Demo", JOptionPane.PLAIN_MESSAGE);
                    System.exit(0);
                }
            });
    
        }
    
        private static class Worker extends Thread {
            final String name;
            final WorkerPauseManager pauseManager;
    
            public Worker(final String name, final WorkerPauseManager pauseManager) {
                this.name = name;
                this.pauseManager = pauseManager;
            }
    
            @Override
            public void run() {
                while (!Thread.interrupted()) {
                    try {
                        pauseManager.pauseIfNeeded();
                        System.out.println(name + " is running");
                        Thread.sleep(1000L);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to wait for queued threads before execute another process?
I was wondering if there's a way to wait for a file to be
Is there a way in PHP to make HTTP calls and not wait for
In Erlang is there any way that a message sender can wait on a
I'm curious if there is an efficient way to wait for the front page
Possible Duplicate: C# Spawn Multiple Threads for work then wait until all finished I
I just want my main thread to wait for any and all my (p)threads
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or
is there way how to get name ov event from Lambda expression like with

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.