私はThreadPoolExecutorをセットアップし、ブロッキングキューからデータを消費するスレッドを開始します。 起動時(以下のstartThreadを呼び出すと)、ブロックキューは空です。 私はスレッドが死ぬことがないようにスレッドが非常に大きくなるようにタイムアウトを設定しました。 ブロッキングキューはWorkerThreadPoolExecutorのスコープ外で作成され、Runnable項目がその上に置かれます。ThreadPoolExecutorはデータを消費しません
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であると思われます。 –