Java 多线程之ThreadFactory

2021/4/17 12:26:59

本文主要是介绍Java 多线程之ThreadFactory,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

ThreadFactory

当线程池中需要创建线程时就会调用该方法

package com.Executor;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadFactory01 {

    public static void main(String[] args) throws InterruptedException {
        Runnable rt = new Runnable() {
            @Override
            public void run() {
                System.out.println("--------------------");
            }
        };
        // 创建线程池
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5, 2, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                System.out.println("]]]]]]]]]]]]]]]]]]]");
                return new Thread(r);

            }
        });

        Runnable r00 = new Runnable() {
            @Override
            public void run() {
                System.out.println("zhng");
            }
        };
        for (int i = 0; i < 5; i++) {
            executor.submit(r00);
        }

        Thread.sleep(10000);
    }
}


这篇关于Java 多线程之ThreadFactory的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程