カスタム・スレッド・工場:5のJavaの並行処理シリーズ

問題

我々は通常、最終的には、スレッドプールのスレッドの内部をクリアしたい、フィールド上の問題を見つけて解決するときタスクが完了されているものは、対応するスレッドのスレッドの名前を指定する必要があります。

ソリューション

スレッドプールを作成するとき、私たちは、植物がマルチスレッドタスクの名前はここにあるコンクリートを完了することが何であるか、トラブルシューティングを行うときに私たちが知っていることを、スレッドのスレッド名を示している可能性があり、カスタムスレッドファクトリを使用することができ、容易にするために、問題とトラブルシューティングを見つけることが、その後の。

public class CustomThreadFactory implements ThreadFactory {


    /**
     *  线程号
     */
    private final AtomicInteger threadNum = new AtomicInteger(1);

    /**
     *  线程名称
     */
    private String threadName;

    public DetectorThreadFactory(String threadName) {
        this.threadName = threadName + "-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        thread.setName(threadName + threadNum.getAndIncrement());
        return thread;
    }
}
88元記事公開 ウォン称賛49 ビューに10万+を

おすすめ

転載: blog.csdn.net/Diamond_Tao/article/details/103459618