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

change _id string to base64 encoded string in mongodb via mongoose

node.js mongodb mongoose base64

By default mongodb generates some random string like 5548087025c7f79259525eff its type is ObjectId. Instead of that random string i want to use base64 encoded string so that it will always start from

Socket read()ing into a function that returns pointer in C++

c++ function sockets

I have a class called message. Here you could find the relevant piece of code:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.