编程语言
首页 > 编程语言> > Java 多线程之ThreadFactory

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,Runnable,Java,ThreadFactory,import,new,多线程,public
来源: https://blog.csdn.net/qq_49644209/article/details/115790427