异步线程池

99 阅读1分钟

demo

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

public class Test {
    private static ExecutorService threadService = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
            60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(), r -> {
        Thread thread = new Thread(r);
        thread.setName("task-flowLimit" + thread.getName());
        return thread;
    });

    public static void main(String[] args) {
        System.out.println("主线程开始");
        try{
            threadService.submit(()->{
                try {
                    System.out.println("子线程开始");
                    Thread.sleep(10000);
                    for (int i = 0; i < 10; i++) {
                        System.out.println("子线程"+i);
                    }
                    System.out.println("子线程结束");
                }catch (Exception e){
                    e.getMessage();
                }
            });
        }catch (Exception e){
            e.getMessage();
        }
        System.out.println("主线程结束");
    }
}