java wait all threads in pool

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{
    waiter=new CountDownLatch(2); // to wait
    robot1=new Robot(0,560,"rbt1"); // starts random free position
    robot2=new Robot(0,560,"rbt2");



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()) {

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;

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()) {
                        } else {
                JOptionPane.showMessageDialog(null, playPauseButton, "WorkerPauseManager Demo", JOptionPane.PLAIN_MESSAGE);


    private static class Worker extends Thread {
        final String name;
        final WorkerPauseManager pauseManager;

        public Worker(final String name, final WorkerPauseManager pauseManager) {
   = name;
            this.pauseManager = pauseManager;

        public void run() {
            while (!Thread.interrupted()) {
                try {
                    System.out.println(name + " is running");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);

You can wait for all the threads to finish with:

pool.awaitTermination(60, TimeUnit.SECONDS); // hopefully 60 seconds are enough


Need Your Help

Problem with HTML Parser in IE

javascript html-parsing

I am trying to create a dialog box that will appear only if the browser selected is IE (any version) however I get this error:

Bootstrap button with divider

css web-applications twitter-bootstrap

I would like to create a bootstrap button with a divider inside of it.

RemoteIO Audio Problem - simulator = good - device= bad

iphone c core-audio audiounit

O.K , so I'm using core audio to extract audio from 10 different sample sources and then mixing them together in my callback function.