2017-06-15 27 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

兩件事情:

  • 在每次調用emitStats()期間絕對不需要創建新的執行器服務。相反,這與這種服務的整個想法相矛盾。
  • 您的示例中沒有代碼會實際消耗tgat隊列中的元素。打印其大小不會改變隊列!
+0

1.我添加了emitStats以查看隨着時間的推移發生了什麼 2.放入阻塞隊列的項目是可運行的;不應該將隊列中的線程池阻塞,並且線程會從隊列中獲取並從隊列中取出項目,並在它將其出隊後執行它? –

+0

線程完成您指示他們執行的任何操作。再說一遍:你沒有顯示任何可以做到的代碼。計算機不能用魔法工作 - 他們確實按照你的代碼告訴他們做的事情。 – GhostCat

+0

問題似乎在這裏,池大小爲0,我似乎無法(重新啓動它們)。 –

相關問題