ThreadPoolExecutor不消耗数据

时间:2017-06-15 04:14:33

标签: java threadpoolexecutor

我已经设置了一个ThreadPoolExecutor并启动线程来使用阻塞队列中的数据。 在启动时(当我在下面调用startThread时),阻塞队列为空。 我已经将线程的超时设置得非常大,以免它们死掉。 阻塞队列是在WorkerThreadPoolExecutor范围之外创建的,并且Runnable项目放在它上面。

public class WorkerThreadPoolExecutor extends ThreadPoolExecutor {

    private final MyBlockingQueue<MyRunnable> blockingQueue;
    private ScheduledExecutorService statsExecutor = null;

    public WorkerThreadPoolExecutor(MyBlockingQueue myBlockingQueue) {

        super(5, 10, 5, TimeUnit.MINUTES, myBlockingQueue);
        this.blockingQueue = myBlockingQueue;

    }

    @Override
    public void shutdown() {
        logger.info("Shutting down the stats emitter!");
        super.shutdown();
        if (statsExecutor != null) {
            statsExecutor.shutdown();
        }
    }

    public void startThreads() {
        logger.info("Starting the WorkerThreadPoolExecutor!!!");
        this.prestartCoreThread();
        emitStats();
    }

    public void numThds() {
        System.err.println("\t\t active: " + this.getActiveCount());
        System.err.println("\t\t completed taskCount: " + this.getCompletedTaskCount());
        System.err.println("\t\t core: " + this.getCorePoolSize());
        System.err.println("\t\t poolsize: " + this.getPoolSize());
        System.err.println("\t\t taskCount: " + this.getTaskCount());
        System.err.println("\t\t Q-Size: " + this.getQueue().size());

        //System.err.println("X Size is: -------------> " + blockingQueue.currentSize());
        System.err.println("X Size is: -------------> " + blockingQueue.getBlockingQueue().size());
        System.err.println("X Size is: -------------> " + this.getQueue().size());
    }

    public void emitStats() {

        this.statsExecutor = Executors.newScheduledThreadPool(1);

        final Runnable emitStats = new Runnable() {
            public void run() {
                System.err.println("Stats id: " + blockingQueue.id);
                //System.err.println("Size is: -------------> " + blockingQueue.currentSize());
                System.err.println("Stats size is: -------------> " + blockingQueue.getBlockingQueue().size());

                numThds();
            }
        };
        statsExecutor.scheduleAtFixedRate(emitStats, 2, 2, TimeUnit.SECONDS);
    }

}

阻止队列是在上面的范围之外创建的,并且项目放在它上面:

BlockingQueue<MyRunnable> blockingQueue = new LinkedBlockingQueue()

项目被添加到要处理的队列中,但它们永远不会出列。 我添加了产生以下结果的统计信息的指标:

Stats size is: -------------> 2
     active: 0
     completed taskCount: 0
     core: 5
     poolsize: 0
     taskCount: 2
     Q-Size: 2
X Size is: -------------> 2
X Size is: -------------> 2

如何强制将项目从阻塞队列中删除并执行?

MyRunnalbe的代码是:

public class MyRunnable implements Runnable {
    private int x;
    public MyRunnable(int x) {
        this.x = x;
    }
    public void run() {
        System.out.println("----> " + x);
    }
}

我通过调用:

创建它的实例
 MyRunnable mr = new MyRunnable(3);

并通过调用

加入队列
blockingQueue.add(mr);

1 个答案:

答案 0 :(得分:1)

两件事:

  • 在每次调用emitStats()期间,绝对不需要创建新的执行程序服务。相反,这与这种服务的整体思想相矛盾。
  • 您的示例中没有代码实际上会消耗来自tgat队列的元素。打印它的大小不会改变队列!