69

で現在実行されているアクティブなスレッドの数を判断する方法はありExecutorServiceますか?

4

6 に答える 6

73

ThreadPoolExecutor実装を使用し、その上でgetActiveCount()を呼び出します。

int getActiveCount() 
// Returns the approximate number of threads that are actively executing tasks.

ExecutorService インターフェイスはそのためのメソッドを提供しません。実装に依存します。

于 2008-09-17T07:30:37.497 に答える
29

poolが ExecutorService インスタンスの名前であると仮定します。

if (pool instanceof ThreadPoolExecutor) {
    System.out.println(
        "Pool size is now " +
        ((ThreadPoolExecutor) pool).getActiveCount()
    );
}
于 2013-09-01T23:30:19.557 に答える
25

ソースコードを確認してくださいExecutors.newFixedThreadPool()

return new ThreadPoolExecutor(nThreads, nThreads,
                              0L, TimeUnit.MILLISECONDS,
                              new LinkedBlockingQueue<Runnable>());

ThreadPoolExecutorにはgetActiveCount()メソッドがあります。したがって、ExecutorServiceをThreadPoolExecutorにキャストするか、上記のコードを直接使用して取得することができます。その後、を呼び出すことができますgetActiveCount()

于 2008-09-17T07:34:34.397 に答える
10

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());
    }
}
于 2008-09-17T07:43:26.933 に答える
4

私は同じ問題を抱えていたので、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));
于 2016-07-04T10:48:37.663 に答える
-5

スレッドがアクティブ化および非アクティブ化されるたびに更新される静的揮発性カウンターをスレッドに配置します。また、API も参照してください。

于 2008-09-17T07:31:54.543 に答える