java 等待池,Java等待池中的所有线程

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

}

}

解决方案

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

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值