zoukankan      html  css  js  c++  java
  • java 自定义线程池

    import lombok.Getter;

    import javax.annotation.PostConstruct;
    import java.util.concurrent.*;
    //创建一个线城池
    public class LogOptionThreadPool {
    private static final ThreadFactory mThreadFactory = new ThreadFactory() {
    //线程的名字
    public Thread newThread(Runnable r) {
    return new Thread(r,"OperationLogThread");
    }
    };
    BlockingQueue<Runnable> workQueue=new LinkedBlockingDeque<>(1000);

    @Getter
    ThreadPoolExecutor logThread;

    @PostConstruct
    public void init(){
    logThread=new ThreadPoolExecutor(2,2,60, TimeUnit.SECONDS,workQueue,
    mThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
    logThread.prestartAllCoreThreads();
    }
    }

    //在service中注入该线程,调用execute方法,开启线程,线程里面放入参数
    @Service
    @RequiredArgsConstructor
    public class LogOptionalService {
    // 异步保存操作记录
    private final LogOptionThreadPool logOptionThreadPool;

    public void doLog(T t){
         //参数目前都是随便写的
    logOptionThreadPool.getLogThread().execute(new OptionRun(t));
    }
    }

    在实现类中,继承 Runnable 接口,然后在run方法里面实现具体的逻辑
    public class OptionRun implements Runnable{
    private Integer teamId;
    private String operationName;
    private String operation;

    public OptionRun(Integer teamId, String operationName, String operation) {
    this.teamId = teamId;
    this.operationName = operationName;
    this.operation = operation;
    }

    public void run() {
    //处理逻辑
    //insert(teamId,operationName,operation);
    }
    }



  • 相关阅读:
    C#时间格式转换问题(12小时制和24小时制)
    ajax跨域请求webservice webconfig配置
    C#时间戳转化为DateTime
    C#生成缩略图
    codeforces-1348-C Phoenix and Distribution
    P4314 CPU监控
    YangK's dfs序与树链剖分
    Yangk's 静态主席树
    P2253 好一个一中腰鼓!
    codeforces-1341D-Nastya and Scoreboard 记忆化搜索
  • 原文地址:https://www.cnblogs.com/foreverstudy/p/15095867.html
Copyright © 2011-2022 走看看