线程池是执行像轮询是?
-
11-09-2019 - |
题
Java的执行者是(据我所知)在线程池概念的抽象 - 的东西,可以接受和执行(执行)任务
我在寻找一个类似的例外轮询概念。我需要不断地查询出特定的队列(离队)项(其中的不落实的BlockingQueue
),执行权和睡眠,并重复这一切,直到关机。
有没有现成的抽象或者我应该写的东西我自己?
(更好的标题的建议,欢迎)
解决方案
轮询是容易的:
Thread t = new Thread(new Runnable() {
public void run() {
try {
while (!t.isInterrupted()) {
Object item;
while ((item = queue.take()) == null) {//does not block
synchronized (lock) { lock.wait(1000L) } //spin on a lock
}
//item is not null
handle(item);
}
} catch (InterruptedException e) { }
}
});
t.start();
也许你需要改一下你的问题,我不太清楚它到底是什么你想干什么?
不隶属于 StackOverflow