スレッドプールの状況

先日書いたサンプル、
oboe2uran.hatenablog.com
のようなケースで、待ち合わせ時にスレッドプールがどういう状況かを知りたい場合がある。

単純であるが、Executors.newFixedThreadPool で作成したものを ThreadPoolExecutor でキャストすれば良い。

ThreadPoolExecutor es = (ThreadPoolExecutor)Executors.newFixedThreadPool(4);

これで、

es.getPoolSize();     // プール内の現在のスレッド数
es.getCompletedTaskCount();  // これまでに実行が完了したタスクのおよその総数
es.getActiveCount();   // アクティブにタスクを実行しているスレッドのおよその数