美文网首页
并发 推送任务

并发 推送任务

作者: cubotudo | 来源:发表于2018-08-09 17:52 被阅读0次

一个调度线程 

线程池 多个线程执行推送信息service

用ScheduledExecutorService 每2秒监控调度线程以及线程池状态

使用CountDownLatch 闭锁等待调度线程以及线程池执行完推送任务 后执行主线程业务逻辑


private void start(SendTask sendTask)throws Exception {

//        CountDownLatch latch = new CountDownLatch(1);

        CountDownLatch threadPoolLatch =new CountDownLatch(1);

CountDownLatch dispatchLatch =new CountDownLatch(1);

String msgContent = sendTask.getMsgContent();

String sendConfId =sendTask.getProviderId();

if(StringUtils.isNotEmpty(sendConfId)){

ProviderSendConf providerSendConf =providerSendConfRepository.findOne(new Integer(sendConfId));

if(providerSendConf  !=null){

String sendService = providerSendConf.getSendService();

if(StringUtils.isNotEmpty(sendService)){

sendMsgService = (MsgSendService) SpringContextUtil.getBean(sendService);

}

}

}

ScheduledExecutorService scheduledService = Executors.newScheduledThreadPool(1);

ExecutorService taskProcessPool =new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),5000,60L, TimeUnit.SECONDS,

new LinkedBlockingQueue(10000));

new Thread(new Runnable() {

@Override

            public void run() {

try {

dispatchLatch.await();

threadPoolLatch.await();

}catch (InterruptedException e) {

e.printStackTrace();

}

logger.info("task end");

System.out.println("task end");

scheduledService.shutdownNow();

}

}).start();

new Thread(new Runnable() {

public boolean isAllSend =false;

@Override

            public void run() {

while (!isAllSend) {

List sendRecordList =sendRecordRepository.findBySendStatusAndFileId(SendStatus.UN_SEND.getStatus(),new Integer(sendTask.getFileId()));

if(sendRecordList !=null &&  sendRecordList.size()>0){

for (SendRecord sendRecord:sendRecordList) {

sendRecord.setSendStatus(SendStatus.SEND.getStatus());

sendRecord.setSendTime(new Date());

sendRecordRepository.save(sendRecord);

Msg msg =new Msg();

msg.setContent(msgContent);

msg.setMobile(sendRecord.getMobile());

MsgSendTask task =new MsgSendTask();

task.setSendService(sendMsgService);

task.setMsg(msg);

taskProcessPool.submit(task);

try {

Thread.sleep(1);

}catch (InterruptedException e) {

e.printStackTrace();

}

}

}else {

isAllSend =true;

}

}

dispatchLatch.countDown();

}

}).start();

new Thread(new Runnable() {

@Override

            public void run() {

try {

//等任务派发完成才开始检查线程池状态

                    dispatchLatch.await();

}catch (InterruptedException e) {

e.printStackTrace();

}

ThreadPoolExecutor executor = (ThreadPoolExecutor) taskProcessPool;

if (executor.getQueue().size() ==0 && executor.getActiveCount() ==0) {

executor.shutdownNow();

threadPoolLatch.countDown();

ImportFile importFile =importFileRepository.findOne(new Integer(sendTask.getFileId()));

importFile.setStatus(ImportFileStatus.TASK_FINISH.getStatus());

importFileRepository.save(importFile);

sendTask.setTaskStatus(TaskStatus.FINISH.getStatus());

sendTaskRepository.save(sendTask);

logger.info("ThreadPool monitor exit....");

System.out.println("ThreadPool monitor exit....");

return;

}

logger.info("ThreadPool monitor....");

System.out.println("ThreadPool monitor....");

scheduledService.schedule(this,2, TimeUnit.SECONDS);

}

}).start();

//        latch.await();

    }

相关文章

  • 并发 推送任务

    一个调度线程 线程池 多个线程执行推送信息service 用ScheduledExecutorService 每2...

  • jmeter批量创建测试数据

    测试场景:回归push推送任务是否正常 操作流程:后台创建push推送任务,待任务推送成功后,检验推送是否正确(备...

  • OS开发多线程篇—NSOperation基本操作

    一、并发数 (1)并发数:同时执⾏行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同...

  • 服务器两种高效的并发模式

    一、并发编程与并发模式 并发编程主要是为了让程序同时执行多个任务,并发编程对计算精密型没有优势,反而由于任务的切换...

  • completablefuture并发任务

    利用allof方法

  • 并行和并发

    并行 Concurrency并行的多个任务是真实的同时执行, 并发 Parallelism并发的多个任务是交替进...

  • 并发编程中的一些基本概念

    技术背景 Concurrency and Parallelism Concurrency:并发是指任务之间可以并发...

  • 并发编程——多线程多进程

    一.并发编程 1.并发 并发:同时接到多个任务,同时执行多个任务,但是具体到某一时刻,只是在执行某一个任务,只是在...

  • 多线程记录

    并发&并行 并发:多个CPU同时执行多个任务. 并行:单个CPU(分配时间片)同时执行多个任务. IllegalT...

  • todo

    短链接生成. 秒杀系统 短链接生成 高并发的红包系统 分布式ID生成 分布式限流 分布式定时任务 新浪微博怎么推送...

网友评论

      本文标题:并发 推送任务

      本文链接:https://www.haomeiwen.com/subject/iitkjftx.html