首页 文章

ThreadPool对Executor来说就像Polling一样?

提问于
浏览
2

Java的Executor(据我所知)是对ThreadPool概念的抽象 - 可以接受和执行(执行)任务的东西 .

我正在为Polling概念寻找类似的例外 . 我需要不断地从特定的队列(没有实现 BlockingQueue )中轮询(出列)项目,执行它们并休眠,并重复所有这些直到关闭 .

是否有现成的抽象或我应该自己写点什么?

(欢迎提出更好 Headers 的建议)

1 回答

  • 2

    轮询很容易:

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

    也许你需要重新解释你的问题,因为我不太确定你正在尝试做什么?

相关问题