自定义线程池

package com.example.disrupto_xdp.quiklystart;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

public class Main {
    private static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        // 参数准备工作
        OrderEventFactory eventFactory = new OrderEventFactory();
        int ringBufferSize = 4;
        //自定义线程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(5,
                Runtime.getRuntime().availableProcessors(),
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(200),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        Thread thread = new Thread(r);
                        thread.setName("order-thread");
                        if (thread.isDaemon()) {
                            thread.setDaemon(false);
                        }
                        if (Thread.NORM_PRIORITY != thread.getPriority()) {
                            thread.setPriority(Thread.NORM_PRIORITY);
                        }
                        return thread;
                    }
                },
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        logger.error(r.toString());
                    }
                });
    }
}

 

你可能感兴趣的:(编码基本功)