当前位置: 首页 > news >正文

网站建设平台合同模板seo免费资源大全

网站建设平台合同模板,seo免费资源大全,wordpress评论开启,手机网站建设方案本文介绍两种BlockingDeque在多线程任务处理时正确结束的方法 一般最开始简单的多线程处理任务过程 把总任务放入BlockingDeque创建多个线程,每个线程内逻辑时,判断BlockingDeque任务是否处理完,处理完退出,还有任务就BlockingDe…

本文介绍两种BlockingDeque在多线程任务处理时正确结束的方法

在这里插入图片描述

一般最开始简单的多线程处理任务过程

  • 把总任务放入BlockingDeque
  • 创建多个线程,每个线程内逻辑时,判断BlockingDeque任务是否处理完,处理完退出,还有任务就BlockingDeque.take()取任务处理
  • 主线程join等待多线程处理完,收尾处理完成任务。

最开始版本代码,10个任务,3个线程来处理

package org.example;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;public class Main3 {public static void main(String[] args){System.out.println("start");BlockingDeque<Integer> task = new LinkedBlockingDeque<>();for (int i = 0; i < 10; i++) {task.add(i);}List<Thread> workers = new ArrayList<>();for (int i = 0; i < 3; i++) {Thread worker = new Thread(()->{while (true) {Integer data = null;try {if (task.size()==0) {System.out.println(Thread.currentThread().getName() +" quit");break;}
//                        Thread.sleep(100); // 默认任务耗时data = task.take();} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() +" do "+ data);}});workers.add(worker);worker.start();}for (Thread worker: workers) {try {worker.join();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("job done");}
}

运行之后,感觉非常好,完美实现逻辑

但是当把上面的任务数加到200,线程数加到30,上面线程sleep的注释打开,再次运行,就会发现主进程最后会被一直卡着不结束,说明多线程没有正确判断任务结束,线程不安全

上面的子线程内的size()等于0到下面的BlockingDeque.take()取任务这段之间的代码,这段不是线程安全的

让线程正确判断任务结束,而且要线程安全的三种方法,推荐第二种,兼顾效率和兼容正确性

  • 判断任务结束这段代码加synchronized约束起来,实现线程安全(太慢)
  • 给总任务task内,加入和线程相同数量的停止标志marker
  • 使用BlockingDeque.poll(超时时间) + 异常数据检查(需要检查异常数据)

使用synchronized约束

package org.example;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;public class Main {public static void main(String[] args) {System.out.println("start");BlockingDeque<Integer> task = new LinkedBlockingDeque<>();for (int i = 0; i < 20; i++) {task.add(i);}List<Thread> workers = new ArrayList<>();for (int i = 0; i < 3; i++) {Thread worker = new Thread(()->{while (true) {Integer data = null;synchronized (task) {if (task.size() ==0) {System.out.println(Thread.currentThread().getName() +" quit");break;}try {data = task.take();} catch (InterruptedException e) {throw new RuntimeException(e);}}try {Thread.sleep(300);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() +" do "+ data);}});workers.add(worker);worker.start();}for (Thread worker: workers) {try {worker.join();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("job done");}
}

总任务添加stop marker停止标志

package org.example;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;public class Main2 {public static void main(String[] args){System.out.println("start");BlockingDeque<Integer> task = new LinkedBlockingDeque<>();for (int i = 0; i < 20; i++) {task.add(i);}List<Thread> workers = new ArrayList<>();for (int i = 0; i < 3; i++) task.add(99);for (int i = 0; i < 3; i++) {Thread worker = new Thread(()->{while (true) {Integer data = null;try {data = task.take();if (data == 99) {System.out.println(Thread.currentThread().getName() +" quit");break;}} catch (InterruptedException e) {throw new RuntimeException(e);}try {Thread.sleep(300);} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() +" do "+ data);}});workers.add(worker);worker.start();}for (Thread worker: workers) {try {worker.join();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("job done");}
}

使用BlockingDeque.poll(超时时间),避免了take的永久性等待问题,但是会取到null值,要加判断处理

package org.example;import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;public class Main4 {public static void main(String[] args){System.out.println("start");BlockingDeque<Integer> task = new LinkedBlockingDeque<>();for (int i = 0; i < 200; i++) {task.add(i);}List<Thread> workers = new ArrayList<>();for (int i = 0; i < 30; i++) {Thread worker = new Thread(()->{while (true) {Integer data = null;try {if (task.size()==0) {System.out.println(Thread.currentThread().getName() +" quit");break;}Thread.sleep(100); // 默认任务耗时data = task.poll(1000, TimeUnit.MILLISECONDS);if (data == null) {System.out.println(Thread.currentThread().getName() +" get null");continue;}} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(Thread.currentThread().getName() +" do "+ data);}});workers.add(worker);worker.start();}for (Thread worker: workers) {try {worker.join();} catch (InterruptedException e) {throw new RuntimeException(e);}}System.out.println("job done");}
}

在这里插入图片描述

http://www.mmbaike.com/news/49620.html

相关文章:

  • 美国做3d+h动画的网站网站优化排名方法有哪些
  • 建站哪家好论坛网站外链推广平台
  • php做简单网站教程seo是什么意思呢
  • 网站的基本建设投资seo相关岗位
  • 宁波营销型网站建设百度推广的方式有哪些
  • 网站备案要关站吗百度一下app下载安装
  • 搞笑资讯网站源码seoaoo
  • 网站 案例展示快速排名优化推广手机
  • 国内做的比较好的旅游网站网站建设黄页
  • 四川建设人才考试官网简述seo
  • 石河子市建设局网站seo最新
  • 做ppt的网站什么广告推广最有效果
  • 怎么做基金公司网站网销怎么做
  • 新手学纪事本html代码做网站优帮云查询数据云查询
  • 做搜狗网站快速排名网站如何进行seo
  • 河北企业建站推广方法有哪几种
  • 做网站开发的公司销售现在百度怎么优化排名
  • 想在中国网站做海外代购链接怎么做
  • 环保设备在那个网站做注册网站
  • 网站开发过程中遇到的问题超级推荐的关键词怎么优化
  • 杭州 建设网站制作十种营销方法
  • 做网站系统源云推广
  • 山东济南网网站建设百度推广业务电话
  • 网站开发公司怎么查百度推广后台登录
  • 中山做网站公司最近三天的国际新闻大事
  • 作品展示网站源码现在有哪些免费推广平台
  • 武汉高端网站建设优化seo优化托管
  • 千博企业网站管理系统营销旗舰版百度一级代理商
  • 威联通nas 做网站百度域名提交收录网址
  • 网站载入页面怎么做青岛专业网站制作