• java线程池


    大神文章:https://www.jianshu.com/p/50fffbf21b39  ;

    总结:
    为了提高并行处理能力,或者想异步处理问题,可以使用线程池;
    线程池减少了线程的创建,但是原先创建的线程会被新加入的任务重新赋值线程变量;

    示例:
     
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Future;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.ThreadFactory;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
     
    import com.google.common.util.concurrent.ThreadFactoryBuilder;
    ///////添加任务
        private static ExecutorService rnrThreadPool = new ThreadPoolExecutor(2, 100,
            60L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(20), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        
        
        public static void main(String[] args) {
         for(int i= 0; i< 100; i++) {
         if(i<2) {
         rnrThreadPool.submit(new RnrCallbackThread2(""+i, false));
         }else{
         rnrThreadPool.submit(new RnrCallbackThread2(""+i, true));
         }
         }
         }
    ////////任务体
    package com.svw.mos.mno.vo.thread;
     
    import java.util.Map;
    import java.util.concurrent.Callable;
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
     
    /**
     * 线程处理如下任务: 1,同步状态到核心层,2,获取基础厂包 3,查车型,设置车型 4,到核心基础场包下单
     */
    public class RnrCallbackThread2 implements Callable<Map<String, String>> {
    private static final Logger LOGGER = LoggerFactory.getLogger(RnrCallbackThread2.class);
    private String reqId;
    private boolean isSuccessRnr;
     
     
    public RnrCallbackThread2(String reqId, boolean isSuccessRnr) {
    this.reqId = reqId;
    this.isSuccessRnr = isSuccessRnr;
     
    }
     
    @Override
    public Map<String, String> call() throws Exception {
    Thread.sleep(2000l);
    LOGGER.info("threadName:{},reqId:{},issuccess:{}", Thread.currentThread().getName(), reqId, isSuccessRnr);
    return null;
    }
     
    }
     
  • 相关阅读:
    poj 1579(动态规划初探之记忆化搜索)
    hdu 1133(卡特兰数变形)
    CodeForces 625A Guest From the Past
    CodeForces 625D Finals in arithmetic
    CDOJ 1268 Open the lightings
    HDU 4008 Parent and son
    HDU 4044 GeoDefense
    HDU 4169 UVALive 5741 Wealthy Family
    HDU 3452 Bonsai
    HDU 3586 Information Disturbing
  • 原文地址:https://www.cnblogs.com/bigjor/p/11759852.html
Copyright © 2020-2023  润新知