阻止队列Take()不检索项目

时间:2015-11-14 15:57:48

标签: java multithreading blockingqueue

我有以下代码:

@Override
public boolean start() {
    boolean b = false;
    if (status != RUNNING) {
        LOGGER.info("Starting Auto Rescheduler Process...");
        try {
            b = super.start();
            final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Rescheduler-Pool-%d").build();
            ExecutorService exServ = Executors.newSingleThreadExecutor(threadFactory);

            service = MoreExecutors.listeningDecorator(exServ);

        } catch (Exception e) {
            LOGGER.error("Error starting Auto Rescheduler Process! {}", e.getMessage());
            LOGGER.debug("{}", e);
            b = false;
        }
    } else {
        LOGGER.info("Asked to start Auto Rescheduler Process but it had already started. Ignoring...");
    }
    return b;
}

AutoRescheduler是可运行的:

private class AutoScheduler implements Runnable {

    private static final String DEFAULT_CONFIGURABLE_MINUTES_VALUE = "other";
    private static final long DEFAULT_DELAY_MINUTES = 60L;

    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                //BLOCKS HERE UNTIL A FINISHED EVENT IS PUT IN QUEUE
                final FinishedEvent fEvent = finishedEventsQueue.take();
                LOGGER.info("Received a finished Event for {} and I am going to reschedule it", fEvent);
                final MyTask task = fEvent.getSource();
                final LocalDateTime nextRunTime = caclulcateNextRightTime(task);
                boolean b = scheduleEventService.scheduleEventANew(task, nextRunTime);
                if (b) {
                    cronController.loadSchedule();
                    LOGGER.info("Rescheduled event {} for {}", task, nextRunTime);
                }
        } catch (InterruptedException e) {
            LOGGER.error("Interrupted while waiting for a new finishedEventQueue");
            Thread.currentThread().interrupt();
        }
    }

我看到事件被捕获并放入队列中。通常我会看到它们被AutoReschduler

重新安排

然而,我不时会看到他们被重新安排,这让我相信reschedulingThread会默默地死去。发生这种情况后,在重新启动进程之前不会再从队列中获取任何事件(我有一个GUI,允许我调用公共类的stop()start()方法)。在我重新启动它之后,阻塞的事件被正常重新安排,这意味着它们确实在队列中。

有没有人有想法?

修改

我在Eclipse中重现了这个错误。线程没有死(我已经使用ExecutorService进行了测试。但是take()仍然没有从队列中取出该项目,尽管它被放置在那里。

0 个答案:

没有答案