加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

异步数据处理

发布时间:2020-12-14 03:23:01 所属栏目:大数据 来源:网络整理
导读:?后台的异步处理 ?把需要处理的数据放入阻塞队列中? public static BlockingQueueJSONObject queue = new LinkedBlockingQueueJSONObject(); AsyncQueue.queue.put(jsonObject); 然后初始化线程池去执行 public static void init(){ executor = new ThreadPo

?后台的异步处理

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

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());         }      }         }}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读