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();
    }


}
Tudor
  • 61,523
  • 12
  • 102
  • 142
Ecrin
  • 246
  • 5
  • 21

2 Answers2

0

You can wait for all the threads to finish with:

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

See: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#awaitTermination%28long,%20java.util.concurrent.TimeUnit%29

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • Thank u. But How I can check whether the button pressed with this method.I understood that it works accroding to a determined wait time. – Ecrin Dec 01 '11 at 16:08
  • @user1073777: You have to put this code in the actionListener of the pause button. This code will wait for all the threads to finish. – Tudor Dec 01 '11 at 16:11
  • ok it is working but for 60 seconds and starting to continue by itself.I want to wait till play button pressed.I mean I cant know when play button pressed and cant write a time instead of '60':( – Ecrin Dec 01 '11 at 16:23
  • No, this method waits until the threads have finished work or 60 seconds have elapsed, whichever comes first. – Tudor Dec 01 '11 at 16:25
0

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);
                }
            }
        }
    }
}
Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
  • I can pause now but cant resume.Can you look over what is wrong? while(true){ if(Frame.pause==true){ try { Thread.currentThread().wait(); } else{ if(end==false){// not reach exit ... run my methods – Ecrin Dec 01 '11 at 17:11
  • You're calling wait() on the thread. Instead, you should call wait() on some arbitrary object. wait() is actually a method in Object, not Thread. The object being waited on should be shared by all workers, and the swing button should call notify() on this object when the state changes. – Sam Barnum Dec 01 '11 at 18:13
  • Yes it works anymore:) Just i changed notifyAll().When used notify() it was only resume current thread.Many thanks:) – Ecrin Dec 01 '11 at 19:41