【发布时间】:2013-12-11 10:09:27
【问题描述】:
我正在实现以下 Java 接口以允许暂停和恢复线程。我有一个使用 wait()/notifyAll() 的工作版本,但我想知道是否有更简单的方法(例如,在 java.util.concurrent 中使用一些漂亮的小部件)?
public interface Suspender {
/**
* Go into pause mode: any threads which subsequently call maybePause()
* will block. Calling pause() if already in pause mode has no effect.
*/
void pause();
/**
* Leave pause mode: any threads which call maybePause() will not block,
* and any currently paused threads will be unblocked. Calling resume()
* if not in pause mode has no effect.
*/
void resume();
/**
* If the Suspender is in pause mode, block, and only resume execution
* when the Suspender is resumed. Otherwise, do nothing.
*/
void maybePause();
}
【问题讨论】:
标签: java multithreading wait notify