質問

そのアイデアからのアクティブスレッドは、現在実行中 ExecutorService?

役に立ちましたか?

解決

を使用 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()
    );
}

チェックをsourcecodeのために執行者.newFixedThreadPool():

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

ThreadPoolExecutorはgetActiveCount()メソッドがあります。ですからキャストのExecutorServiceをThreadPoolExecutor,oder、上記のコードを直接取得します。その呼び出しgetActiveCount().

のExecutorServiceインタフェースは定義しない方法検討のために作業員数のスレッドプールのこの実装の詳細

public int getPoolSize()
Returns the current number of threads in the pool.

は、ThreadPoolExecutorクラス

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class PoolSize {

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue());
        System.out.println(executor.getPoolSize());
    }
}

が必要としている事実が明示的に作成、ThreadPoolExecutorるのではなく、の執行者工場を返しますExecutorService。でき常に新工場が返されThreadPoolExecutorsがないです左手の悪い形のコンクリートタイプではなく、そのインタフェース。

一つの可能性に提供するThreadFactoryを作成するスレッドで知られるスレッドグループでそのサウンド

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;


public class PoolSize2 {

    public static void main(String[] args) {
        final ThreadGroup threadGroup = new ThreadGroup("workers");

        ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {
            public Thread newThread(Runnable r) {
                return new Thread(threadGroup, r);
            }
        });

        System.out.println(threadGroup.activeCount());
    }
}

また同号でシンプルRunnableトレースExecutorServiceインスタンス.

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 ######");
        }

    }
}

利用できることexecutorを得国のThreadPool

Ex

ExecutorService executorService = Executors.newFixedThreadPool(4);
    executorService.execute(new ExecutorServiceAnalyzer(executorService, 1000));

場所静的揮発性のカウンターのスレッドであるたびに更新のスレッドが起動と停止しています。また、APIに含まれています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top