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

b to c网站建设报价新闻摘抄2022最新5篇

b to c网站建设报价,新闻摘抄2022最新5篇,5566网址大全设首页,青岛网站建设公司效果文章目录 1. 定义2. 实现保护性暂停模式3. Join原理4. 保护性暂停模式的扩展 1. 定义 即Guarded Suspension,用在一个线程等待另一个线程的执行结果。 有一个结果需要从一个线程传递给另一个线程,让他们关联到同一个GuarderObject(这就是保…

文章目录

    • 1. 定义
    • 2. 实现保护性暂停模式
    • 3. Join原理
    • 4. 保护性暂停模式的扩展

1. 定义

即Guarded Suspension,用在一个线程等待另一个线程的执行结果。

  • 有一个结果需要从一个线程传递给另一个线程,让他们关联到同一个GuarderObject(这就是保护性暂停模式,是两个线程之间交换结果的模式)
  • 如果有结果不断从一个线程到另一个线程可以使用消息队列(这个是生产者-消费者模式)
  • JDK中,Join实现,Futrue的实现,采用的就是此模式
  • 因为要等待另一方的结果,因此归类到同步模式

在这里插入图片描述

2. 实现保护性暂停模式

实现这个模式的关键是GuardedObject,response属性是用来保存中间结果。所以我们使用wait-notify来实现保护性暂停模式。

实现保护对象

class  GuardedObject{private Object response;//获取结果public Object get() {synchronized (this){while(response==null){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}return  response;}}public void complete(Object response){synchronized (this){this.response=response;this.notify();}}
}

案例场景,线程1等待线程二的下载结果

public class jvm {public static List<String> downLoad() throws IOException {HttpURLConnection connection= (HttpURLConnection) new URL("https://www.baidu.com/").openConnection();List<String> list=new ArrayList<>();try(BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))){String line;while((line= reader.readLine())!=null){list.add(line);}}return list;}public static void main(String[] args) {GuardedObject guardedObject=new GuardedObject();new Thread(()->{log.debug("等待结果");List<String> list= (List<String>) guardedObject.get();log.debug("结果大小,[{}]",list.size());},"线程1").start();new Thread(()->{log.debug("执行下载");try {List<String> list=downLoad();guardedObject.complete(list);} catch (IOException e) {e.printStackTrace();}},"线程2").start();}
}

在这里插入图片描述

3. Join原理

Join底层原理就是基于这种保护性暂停的模式,首先我们来看看Join的底层源码

public final synchronized void join(long millis)throws InterruptedException {//获得系统当前的时间戳long base = System.currentTimeMillis();//定义当前时间戳为0long now = 0;if (millis < 0) {throw new IllegalArgumentException("timeout value is negative");}//如果传入的等待时间为0if (millis == 0) {//如果线程是存活的就一直等待,调用wait(0)while (isAlive()) {wait(0);}} else {while (isAlive()) {long delay = millis - now;if (delay <= 0) {break;}wait(delay);now = System.currentTimeMillis() - base;}}}public final synchronized void join(long millis, int nanos)throws InterruptedException {if (millis < 0) {throw new IllegalArgumentException("timeout value is negative");}if (nanos < 0 || nanos > 999999) {throw new IllegalArgumentException("nanosecond timeout value out of range");}if (nanos >= 500000 || (nanos != 0 && millis == 0)) {millis++;}join(millis);}public final void join() throws InterruptedException {join(0);}

从源码可以看出,join的底层就是使用wait机制实现的。

4. 保护性暂停模式的扩展

途中Futures就好比居民楼的一层信箱(每个信箱都有自己的编号),左侧的t0,t2,t4就好比等待邮件的居民(等待结果的线程),右侧t1,t3,t5就好比邮递员。如果需要在多个类之间使用GuardedObject对象,作为参数传递不是很方便,因此设计一个用来解耦的中间类,这样不仅仅能够解藕结果等待者和结果生产者,还能支持多个任务的管理。

在这里插入图片描述

改造GuardedObject类

class  GuardedObject{private Object response;private int id;public GuardedObject(){}public GuardedObject(int id){this.id=id;}public int getId(){return id;}//获取结果public Object get() {synchronized (this){while(response==null){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}return  response;}}public void complete(Object response){synchronized (this){this.response=response;this.notify();}}
}

构造解耦类

class Boxes{private static Map<Integer,GuardedObject> box=new ConcurrentHashMap<>();//产生一个唯一的idpublic static int id=1;private static synchronized int increment(){return id++;}public static  GuardedObject getGuardedObject(int id){return box.remove(id);}public static GuardedObject creatGuardedObject(){GuardedObject guardedObject=new GuardedObject(increment());box.put(guardedObject.getId(),guardedObject);return guardedObject;}public static Set<Integer> getIds(){return box.keySet();}}

创造等待线程和生产线程

@Slf4j
class  PostMan extends Thread{private int id;private String mail_contex;//邮递员创建信件public PostMan(int id,String mail_contex){this.id=id;this.mail_contex=mail_contex;}@Overridepublic void run(){GuardedObject guardedObject=Boxes.getGuardedObject(id);log.debug("送信-{},内容-{}",id,mail_contex);guardedObject.complete(mail_contex);}
}
class Boxes{private static Map<Integer,GuardedObject> box=new Hashtable<>();//产生一个唯一的idpublic static int id=1;private static synchronized int increment(){return id++;}public static  GuardedObject getGuardedObject(int id){return box.remove(id);}public static GuardedObject creatGuardedObject(){GuardedObject guardedObject=new GuardedObject(increment());box.put(guardedObject.getId(),guardedObject);return guardedObject;}public static Set<Integer> getIds(){return box.keySet();}}

测试

public class jvm {public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 3; i++) {new Poeple().start();}Thread.sleep(1000);for (Integer id : Boxes.getIds()) {new PostMan(id, "内容" + id).start();}}
}

在这里插入图片描述

http://www.tj-hxxt.cn/news/84570.html

相关文章:

  • 六安市城乡和建设局官方网站石家庄seo推广公司
  • 网站如何取消限制搜索引擎抓取自建网站平台
  • 濉溪县最新通告今天seo竞价排名
  • 网站怎么实现手机号注册会员怎样在百度上发布广告
  • 南宁做网站 的大批量刷关键词排名软件
  • 平台设计方案seo关键词排名优化哪家好
  • 公司邮箱价格seo推广主要做什么的
  • 中文网站模板seo的基本内容
  • 电脑网站被劫持怎么恢复建站系统哪个比较好
  • 优秀网站设计案例中国搜狗收录提交入口
  • 怎么做原创电影视频网站个人网站开发网
  • 合肥建设集团信息网站发表文章的平台有哪些
  • 做网站怎么入账百度推广退款电话
  • 一个人做两个博客网站江苏搜索引擎优化公司
  • 找人做网站深圳seo关键词优化外包公司
  • 宜城网站建设深圳网络营销推广服务
  • seo公司排行seo排名赚挂机
  • 徐汇科技网站建设蜜雪冰城网络营销案例分析
  • 网站建设合同 下载福州网站建设
  • 网络服务商怎么查询东莞seo网站排名优化
  • 28网站制作seo网络营销推广
  • 永嘉网站制作系统seo优化范畴
  • 东西湖区网站建设公司今日国内新闻头条
  • 网络销售怎么做网站旺道seo营销软件
  • 成都公司网站开发交换友情链接的意义是什么
  • 衢州体育中心设计理念小红书关键词排名优化
  • mvc做的游戏网站关键词排名推广
  • 深圳品牌网站制作报价爱站网关键词密度
  • 聊城做网站厉害的公司杭州关键词优化平台
  • 佛山论坛建站模板福州seo推广外包