【发布时间】:2015-02-20 16:16:20
【问题描述】:
多个工作线程正在从一个队列进行处理,当数据库发生故障时,它将联系主管,然后锁定所有工作线程并定期轮询数据库,直到数据库启动,然后释放所有线程以便它们可以继续处理。工作线程可以提前或等待处理,主管线程可以锁定或解锁。
我在想这样的界面。你会使用什么同步原语?演员会是一个很好的解决方案,但我没有时间重写。
public interface Latch {
/**
* This method will cause a thread(s) to only advance if the latch is in an open state. If the
* latch is closed the thread(s) will wait until the latch is open before they can advance.
*/
void advanceWhenOpen();
/**
* Close the latch forcing all threads that reaches the latch's advance method to wait until
* its open again.
*/
void close();
/**
* Opens the latch allowing blocked threads to advance.
*/
void open();
boolean isOpen();
}
【问题讨论】:
-
可能这个问题应该去 CodeReview。
-
我还没有实现它,我正在寻找解决问题的不同策略。
-
@mkrakhin CodeReview 需要审查一个有效的实现,这只是一个接口的(草稿版本)。
-
好吧。看来我对CR有一些误解。对不起。
-
您使用的是哪种队列?
BlockingQueue会为你做这件事。
标签: java multithreading