手动写一个异步线程池

2020-07-03 13:33:00
admin
原创
542
摘要:手动写一个异步线程池

手动写一个异步线程池

import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
/**
 * Author:zhansan
 * Date:2020/7/3
 * Description:
 */
@Slf4j
public class AsyncBaseQueue {
    private static final int THREAD_SIZE=Runtime.getRuntime().availableProcessors();
    private static final int QUEUE_SIZE=1000;
    private static ExecutorService senderAsync= new ThreadPoolExecutor(
            THREAD_SIZE, THREAD_SIZE, 60L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(THREAD_SIZE),
            new ThreadFactory() {
                @Override
                public Thread newThread(Runnable r) {
                    Thread thread = new Thread(r);
                    thread.setName("队列名字");
                    return thread;
                }
            }, new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
          log.info("async sender is error rejected,runnable:{},executor:{}",r,executor);
        }
    }
    );
    public static void submit(Runnable runnable){
        senderAsync.submit(runnable);
    }

    public static void main(String[] args) {
        AsyncBaseQueue.submit((Runnable)()->{
             log.info("#####调用方法########");
        });
    }
} 
发表评论
评论通过审核之后才会显示。
文章分类
联系方式
联系人: 郑州-小万
电话: 13803993919
Email: 1027060531@qq.com
QQ:
网址: www.blizzardjewelry.com