で現在実行されているアクティブなスレッドの数を判断する方法はありExecutorService
ますか?
6 に答える
ThreadPoolExecutor実装を使用し、その上でgetActiveCount()を呼び出します。
int getActiveCount()
// Returns the approximate number of threads that are actively executing tasks.
ExecutorService インターフェイスはそのためのメソッドを提供しません。実装に依存します。
pool
が ExecutorService インスタンスの名前であると仮定します。
if (pool instanceof ThreadPoolExecutor) {
System.out.println(
"Pool size is now " +
((ThreadPoolExecutor) pool).getActiveCount()
);
}
ソースコードを確認してくださいExecutors.newFixedThreadPool()
:
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
ThreadPoolExecutorにはgetActiveCount()メソッドがあります。したがって、ExecutorServiceをThreadPoolExecutorにキャストするか、上記のコードを直接使用して取得することができます。その後、を呼び出すことができますgetActiveCount()
。
ExecutorService インターフェイスは、プール内のワーカー スレッドの数を調べるメソッドを定義していません。これは実装の詳細であるためです。
public int getPoolSize()
Returns the current number of threads in the pool.
ThreadPoolExecutor クラスで利用可能
java.util.concurrent.LinkedBlockingQueue をインポートします。 java.util.concurrent.ThreadPoolExecutor をインポートします。 import java.util.concurrent.TimeUnit; パブリック クラス プールサイズ { public static void main(String[] args) { ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue()); System.out.println(executor.getPoolSize()); } }
ただし、これには、ExecutorService オブジェクトを返す Executors ファクトリを使用するのではなく、ThreadPoolExecutor を明示的に作成する必要があります。ThreadPoolExecutors を返す独自のファクトリをいつでも作成できますが、インターフェイスではなく具象型を使用するという不適切な形式が残ることになります。
1つの可能性は、既知のスレッドグループにスレッドを作成する独自の ThreadFactory を提供することです。これをカウントできます
java.util.concurrent.ExecutorService をインポートします。 java.util.concurrent.Executors をインポートします。 java.util.concurrent.ThreadFactory をインポートします。 パブリック クラス PoolSize2 { public static void main(String[] args) { final ThreadGroup threadGroup = new ThreadGroup("workers"); ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() { public Thread newThread(Runnable r) { 新しいスレッド (threadGroup、r) を返します。 } }); System.out.println(threadGroup.activeCount()); } }
私は同じ問題を抱えていたので、ExecutorService インスタンスをトレースする単純な Runnable を作成しました。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
public class ExecutorServiceAnalyzer implements Runnable
{
private final ThreadPoolExecutor threadPoolExecutor;
private final int timeDiff;
public ExecutorServiceAnalyzer(ExecutorService executorService, int timeDiff)
{
this.timeDiff = timeDiff;
if (executorService instanceof ThreadPoolExecutor)
{
threadPoolExecutor = (ThreadPoolExecutor) executorService;
}
else
{
threadPoolExecutor = null;
System.out.println("This executor doesn't support ThreadPoolExecutor ");
}
}
@Override
public void run()
{
if (threadPoolExecutor != null)
{
do
{
System.out.println("#### Thread Report:: Active:" + threadPoolExecutor.getActiveCount() + " Pool: "
+ threadPoolExecutor.getPoolSize() + " MaxPool: " + threadPoolExecutor.getMaximumPoolSize()
+ " ####");
try
{
Thread.sleep(timeDiff);
}
catch (Exception e)
{
}
} while (threadPoolExecutor.getActiveCount() > 1);
System.out.println("##### Terminating as only 1 thread is active ######");
}
}
}
これをエグゼキューターで使用するだけで、ThreadPool の状態を取得できます。
元
ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000));
スレッドがアクティブ化および非アクティブ化されるたびに更新される静的揮発性カウンターをスレッドに配置します。また、API も参照してください。