【问题标题】:ThreadPoolExecutor not consuming dataThreadPoolExecutor 不消耗数据
【发布时间】:2017-11-17 09:54:27
【问题描述】:

我已经设置了一个 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);

【问题讨论】:

  • 似乎活动线程数为 0 是可疑的!

标签: java threadpoolexecutor


【解决方案1】:

两件事:

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

【讨论】:

  • 1.我添加了 emitStats 以查看随着时间的推移发生了什么 2. 放入阻塞队列的项目是可运行的;线程池不应该阻塞队列并且线程从队列中抓取和项目并在它出队后执行它吗?
  • 线程执行您指示它们执行的任何操作。再说一遍:您没有显示任何可以做到这一点的代码。计算机不是靠魔法工作的——它们完全按照你的代码告诉它们去做。
  • 这里的问题似乎是池大小为 0,我似乎无法(重新?)启动它们。
猜你喜欢
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2017-10-28
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2018-03-16
相关资源
最近更新 更多