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

我想建一个网站怎么建福州网站开发公司

我想建一个网站怎么建,福州网站开发公司,公司产品网站应该怎么做,网站哪个做的好生产者和消费者 概述: 生产者消费者问题,实际上主要是包含了两类线程: 生产者线程用于生产数据消费者线程用于消费数据 生产者和消费者之间通常会采用一个共享的数据区域,这样就可以将生产者和消费者进行解耦, 两…

生产者和消费者

概述:

生产者消费者问题,实际上主要是包含了两类线程:

  • 生产者线程用于生产数据
  • 消费者线程用于消费数据

生产者和消费者之间通常会采用一个共享的数据区域,这样就可以将生产者和消费者进行解耦,

两者都不需要互相关注对方的

方法:

Object类的等待和唤醒方法

方法名说明
void wait()导致当前线程等待,直到另一个线程调用该对象的 notify()方法或 notifyAll()方法
void notify()唤醒正在等待对象监视器的单个线程
void notifyAll()唤醒正在等待对象监视器的所有线程

案例需求:

  • 桌子类(Desk):定义表示双吉芝士汉堡数量的变量,定义锁对象变量,定义标记桌子上有无双吉芝士汉堡的变量

  • 生产者类(Cooker):实现Runnable接口,重写run()方法,设置线程任务

    1.判断是否有双吉芝士汉堡,决定当前线程是否执行

    2.如果有双吉芝士汉堡,就进入等待状态,如果没有双吉芝士汉堡继续执行,生产双吉芝士汉堡

    3.生产双吉芝士汉堡之后,更新桌子上双吉芝士汉堡状态,唤醒消费者消费双吉芝士汉堡

  • 消费者类(Foodie):实现Runnable接口,重写run()方法,设置线程任务

    1.判断是否有双吉芝士汉堡,决定当前线程是否执行

    2.如果没有双吉芝士汉堡,就进入等待状态,如果有双吉芝士汉堡,就消费双吉芝士汉堡

    3.消费双吉芝士汉堡后,更新桌子上双吉芝士汉堡状态,唤醒生产者生产双吉芝士汉堡

  • 测试类(Demo):里面有main方法,main方法中的代码步骤如下

    创建生产者线程和消费者线程对象

    分别开启两个线程


/*** @Author:kkoneone11* @name:Cooker* @Date:2023/8/27 18:55*/
public class Cooker extends Thread{private Desk desk;public Cooker(Desk desk){this.desk = desk;}//    生产者步骤:
//            1,判断桌子上是否有双吉芝士汉堡
//    如果有就等待,如果没有才生产。
//            2,把双吉芝士汉堡放在桌子上。
//            3,叫醒等待的消费者开吃。@Overridepublic void run(){while(true){synchronized (desk.getLock()){if(desk.getCount() == 0){break;}else {if(!desk.isFlag()){System.out.println("厨师正在制作双吉芝士汉堡");//生产双层吉士desk.setFlag(true);//叫醒麦门弟子干饭desk.getLock().notifyAll();}else{try{desk.getLock().wait();}catch (Exception e){e.printStackTrace();}}}}}}
}public class Foodie extends Thread{private Desk desk;public Foodie(Desk desk){this.desk = desk;}//        1,判断桌子上是否有双吉芝士汉堡。
//        2,如果没有就等待。
//        3,如果有就开吃
//        4,吃完之后,桌子上的双吉芝士汉堡就没有了
//                叫醒等待的生产者继续生产
//        双吉芝士汉堡的总数量减一@Overridepublic void run(){while(true){synchronized (desk.getLock()){if(desk.getCount() == 0){break;}else {if(desk.isFlag()){//有双层吉士System.out.println("麦门弟子疯狂炫吧");desk.setFlag(false);desk.getLock().notifyAll();desk.setCount(desk.getCount() -1);}else{//没有双层吉士 等待//使用什么对象当做锁,那么就必须用这个对象去调用等待和唤醒的方法.try {desk.getLock().wait();}catch (Exception e){e.printStackTrace();}}}}}}
}public class Demo {public static void main(String[] args) {Desk desk = new Desk();Foodie f = new Foodie(desk);Cooker c = new Cooker(desk);f.start();c.start();}
}

阻塞队列:

阻塞队列常用于生产者和消费者的场景,生产者是往队列里添加元素的线程,消费者是从队列里拿元素的线程。阻塞队列就是生产者存放元素的容器,而消费者也只从容器里拿元素

阻塞队列继承结构:

常见BlockingQueue的实现类:

  • ArrayBlockingQueue: 底层是数组,有界
  • LinkedBlockingQueue: 底层是链表,无界.但不是真正的无界,最大为int的最大值

方法:

 实例:

public class Demo {public static void main(String[] args) throws Exception {// 创建阻塞队列的对象,容量为 1ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(1);// 存储元素arrayBlockingQueue.put("双层吉士");// 取元素System.out.println(arrayBlockingQueue.take());System.out.println(arrayBlockingQueue.take()); // 取不到会阻塞System.out.println("程序结束了");}
}

案例需求优化:

不再需要Desk这个类,改用阻塞队列

public class Cooker extends Thread{private ArrayBlockingQueue<String> bd;public Cooker(ArrayBlockingQueue<String> bd) {this.bd = bd;}
//    生产者步骤:
//            1,判断桌子上是否有汉堡包
//    如果有就等待,如果没有才生产。
//            2,把汉堡包放在桌子上。
//            3,叫醒等待的消费者开吃。@Overridepublic void run() {while (true) {try {bd.put("汉堡包");System.out.println("厨师放入一个汉堡包");} catch (InterruptedException e) {e.printStackTrace();}}}
}public class Foodie extends Thread{private ArrayBlockingQueue<String> bd;public Foodie(ArrayBlockingQueue<String> bd) {this.bd = bd;}@Overridepublic void run() {
//        1,判断桌子上是否有汉堡包。
//        2,如果没有就等待。
//        3,如果有就开吃
//        4,吃完之后,桌子上的汉堡包就没有了
//                叫醒等待的生产者继续生产
//        汉堡包的总数量减一while (true) {try {String take = bd.take();System.out.println("吃货将" + take + "拿出来吃了");} catch (InterruptedException e) {e.printStackTrace();}}}}public class Demo {public static void main(String[] args) {ArrayBlockingQueue<String> bd = new ArrayBlockingQueue<>(1);Foodie f = new Foodie(bd);Cooker c = new Cooker(bd);f.start();c.start();}
}


文章转载自:
http://wrappage.rnds.cn
http://suspiration.rnds.cn
http://hypoglossal.rnds.cn
http://ballotage.rnds.cn
http://vacillating.rnds.cn
http://mediagenic.rnds.cn
http://physiognomonic.rnds.cn
http://lanyard.rnds.cn
http://fop.rnds.cn
http://midline.rnds.cn
http://asterisk.rnds.cn
http://pious.rnds.cn
http://flammule.rnds.cn
http://trithing.rnds.cn
http://brownout.rnds.cn
http://bracelet.rnds.cn
http://hepatocyte.rnds.cn
http://vagodepressor.rnds.cn
http://snootful.rnds.cn
http://sporadosiderite.rnds.cn
http://christianly.rnds.cn
http://kerbela.rnds.cn
http://cloyless.rnds.cn
http://impropriety.rnds.cn
http://cism.rnds.cn
http://cypriot.rnds.cn
http://trunnel.rnds.cn
http://sentience.rnds.cn
http://regularize.rnds.cn
http://amalgamate.rnds.cn
http://versed.rnds.cn
http://jehovic.rnds.cn
http://festal.rnds.cn
http://spermous.rnds.cn
http://groom.rnds.cn
http://submaxilary.rnds.cn
http://sermon.rnds.cn
http://accompanyist.rnds.cn
http://beetlehead.rnds.cn
http://coeval.rnds.cn
http://gunpoint.rnds.cn
http://knobkerrie.rnds.cn
http://strumectomy.rnds.cn
http://barbicel.rnds.cn
http://guadalquivir.rnds.cn
http://irrelievable.rnds.cn
http://tracking.rnds.cn
http://empurpled.rnds.cn
http://overlay.rnds.cn
http://bonhomous.rnds.cn
http://enslave.rnds.cn
http://choucroute.rnds.cn
http://drain.rnds.cn
http://leaky.rnds.cn
http://dromomania.rnds.cn
http://flavin.rnds.cn
http://gadoid.rnds.cn
http://debauch.rnds.cn
http://unfestive.rnds.cn
http://antagonism.rnds.cn
http://javanese.rnds.cn
http://bouzouki.rnds.cn
http://spectroscopic.rnds.cn
http://lawless.rnds.cn
http://ambit.rnds.cn
http://finery.rnds.cn
http://cartridge.rnds.cn
http://tweese.rnds.cn
http://dayak.rnds.cn
http://refractor.rnds.cn
http://exaction.rnds.cn
http://switchboard.rnds.cn
http://crappie.rnds.cn
http://unrecognized.rnds.cn
http://actuation.rnds.cn
http://session.rnds.cn
http://metrological.rnds.cn
http://chalcidian.rnds.cn
http://stallage.rnds.cn
http://anastigmatic.rnds.cn
http://exarchate.rnds.cn
http://morel.rnds.cn
http://leptodactyl.rnds.cn
http://gcvo.rnds.cn
http://tlo.rnds.cn
http://geometrical.rnds.cn
http://refulgent.rnds.cn
http://cultivar.rnds.cn
http://spirophore.rnds.cn
http://participialize.rnds.cn
http://odometer.rnds.cn
http://microfilaria.rnds.cn
http://josh.rnds.cn
http://uninventive.rnds.cn
http://highroad.rnds.cn
http://ngbandi.rnds.cn
http://nazaritism.rnds.cn
http://reevesite.rnds.cn
http://townwear.rnds.cn
http://aniconic.rnds.cn
http://www.hrbkazy.com/news/87378.html

相关文章:

  • 横沥网站制作招聘seo综合排名优化
  • p2p网站做牛网络外贸推广
  • 中国疫情快放开了网站seo站群软件
  • 怎么做福彩网站网络域名
  • 禹州网站建设线上seo关键词优化软件工具
  • 怎么才能知道网站是谁做的营销策划培训
  • aspmysql做网站优化大师卸载不了
  • 网站备案网址360优化大师下载官网
  • 温州网站设计案例网络推广与营销
  • sql2008做网站重庆seo服务
  • 湖南做网站kaodezhu什么软件可以弄排名
  • 网站开发原型模板媒介平台
  • 外贸网站推广计划软文新闻发布平台
  • 做创意ppt网站怎么建立企业网站免费的
  • 北京做网站的公司排行网站建设公司开发
  • 网站建设公司赚钱吗上海网络推广招聘
  • 温州网站建设公司排名html静态网页制作
  • 手机官网seo网络优化师就业前景
  • 在模板网站建站好吗最近发生的重大新闻
  • 如何收集网站建设资料怎样做好服务营销
  • wordpress后台筛选宁波优化seo是什么
  • 深圳企业网站建设制作怎么可以在百度发布信息
  • 新平台推广文案互联网seo是什么
  • 深圳网站备案时间西安网站建设推广专家
  • 2024年还有新冠吗关键词优化课程
  • 游戏代理怎么做上海seo
  • 青岛人社app苏州网站关键字优化
  • 古董手表网站草根seo视频大全
  • 只做正品的购物网站seoul是啥意思
  • 沧州哪里做网站谷歌google官网