2017-06-15 9 views
2

私は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

アクティブスレッド数が0であると思われます。 –

答えて

1

2つのこと:

  • emitStats()を呼び出すたびに新しいexecutorサービスを作成する必要はありません。それどころか、それはそのようなサービスの考え方全体に反する。
  • 実際にはtgatキューの要素を消費するコードは例にはありません。そのサイズを印刷してもキューは変わりません!
+0

1. emitStatsを追加して、何が起きているのかを確認しました。 2.ブロッキングキューに置かれたアイテムは実行可能です。キュー上のスレッドプールブロックとスレッドがキューから取得してアイテムを取り出し、それをデキューしたら実行するべきではありませんか? –

+0

スレッドは、あなたが何をすべきかを指示します。再度:あなたはそれを行うコードを表示していません。コンピュータは魔法の仕組みではありません。コードはあなたのコードが行うことを正確に行います。 – GhostCat

+0

この問題は、ここではプールのサイズが0で、私はそれらを開始することができないようです。 –

関連する問題