zoukankan      html  css  js  c++  java
  • 异步数据处理

     后台的异步处理

     把需要处理的数据放入阻塞队列中 

    public static BlockingQueue<JSONObject> queue = new LinkedBlockingQueue<JSONObject>();
    AsyncQueue.queue.put(jsonObject);


    然后初始化线程池去执行

    public static void init(){
    executor = new ThreadPoolExecutor(100, 200, 30*1000,
    TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(5000));

    log.info("初始化核心线程数量 "+executor.getCorePoolSize());
    log.info("初始化最大线程数量 "+executor.getMaximumPoolSize());

    executor.execute(new SmsAsyncQueueTake(queue));
    }


    public static void destroyed(){
    executor.shutdown();
    }


    线程的设计 需要做的事情
    public class SmsAsyncQueueTake implements Runnable{

    private static Logger log = Logger.getLogger(SmsAsyncQueueTake.class);
    private BlockingQueue<JSONObject> queue;

    public SmsAsyncQueueTake(BlockingQueue<JSONObject> queue){
    this.queue=queue;
    }


    @Override
    public void run() {
    while(!Thread.interrupted()){
    try {
    if(AsyncQueue.executor.getQueue().size()<5000){
    JSONObject sms = queue.take();
    try {
    AsyncQueue.executor.execute(new SmsAsyncService(sms));
    } catch (RejectedExecutionException e) {
    log.debug("异步线程执行异常:"+e.getMessage());
    }

    }else{
    Thread.sleep(500);
    }

    } catch (Exception e) {
    log.error(e.getMessage());
    }
    }

    }

    }




  • 相关阅读:
    逻辑分支中if小括号中的隐式转换
    逻辑运算符
    CSS——元素的显示与隐藏
    JS对象
    JS预解析与变量提升
    JS作用域
    JS函数
    CSS定位(position)
    JS数组
    JS分支结构与循环结构
  • 原文地址:https://www.cnblogs.com/yangyang2018/p/9244728.html
Copyright © 2011-2022 走看看