国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java實(shí)現(xiàn)簡(jiǎn)易生產(chǎn)者消費(fèi)者模型過(guò)程解析

瀏覽:57日期:2022-08-31 16:56:15

一、概述

一共兩個(gè)線程,一個(gè)線程生產(chǎn)產(chǎn)品,一個(gè)線程消費(fèi)產(chǎn)品,使用同步代碼塊方法,同步兩個(gè)線程。當(dāng)產(chǎn)品沒(méi)有時(shí),通知生產(chǎn)者生產(chǎn),生產(chǎn)者生產(chǎn)后,通知消費(fèi)者消費(fèi),并等待消費(fèi)者消費(fèi)完。

需要注意的是,有可能出現(xiàn),停止生產(chǎn)產(chǎn)品后,消費(fèi)者還沒(méi)未來(lái)得及消費(fèi)生產(chǎn)者生產(chǎn)的最后一個(gè)產(chǎn)品,就結(jié)束消費(fèi),導(dǎo)致最后一個(gè)產(chǎn)品沒(méi)有被消費(fèi)。

本例使用synchronize以及wait()、notify()實(shí)現(xiàn)簡(jiǎn)易版的線程者消費(fèi)者模型。

二、測(cè)試用例

這里的產(chǎn)品用筆來(lái)演示,每只筆都有其編號(hào)code

一共有四個(gè)類(lèi):分別是生產(chǎn)者類(lèi),產(chǎn)品類(lèi),消費(fèi)者類(lèi),測(cè)試類(lèi)

Java實(shí)現(xiàn)簡(jiǎn)易生產(chǎn)者消費(fèi)者模型過(guò)程解析

產(chǎn)品

package test.exception.producer_consumer_model;/*假設(shè)為產(chǎn)品為筆 */public class Production { private String type = ''; private String color = ''; private long code = 0; // 產(chǎn)品編號(hào) private boolean isProduced = false; // 是否生產(chǎn)完成 初始狀態(tài)為未生產(chǎn)狀態(tài) private boolean isContinueProduce = true; // 是否停產(chǎn)該產(chǎn)品 public void setContinueProduce(boolean continueProduce) { isContinueProduce = continueProduce; } public void setCode(long code) { this.code = code; } public Production(){ } public boolean isContinueProduce() { return isContinueProduce; } public void setType(String type) { this.type = type; } public void setColor(String color) { this.color = color; } public void setProduced(boolean produced) { isProduced = produced; } public boolean isProduced() { return isProduced; } @Override public String toString() { return color + type + '-' + code; }}

生產(chǎn)者

package test.exception.producer_consumer_model;public class Producer implements Runnable { private final Production pen; // 產(chǎn)品 public Producer(Production pen) { this.pen = pen; } // 生產(chǎn) public void produce() { long code = 0; while (this.pen.isContinueProduce()) { synchronized (this.pen) {if (this.pen.isProduced()) { try { this.pen.wait(); // 等待消費(fèi)者消費(fèi) } catch (InterruptedException e) { e.printStackTrace(); }}// 開(kāi)始生產(chǎn)this.pen.setType('鉛筆');this.pen.setColor('藍(lán)色');this.pen.setCode(code++);this.pen.setProduced(true);System.out.println(this.pen + ' is produced');this.pen.notify(); } } System.out.println('finish producing'); } @Override public void run() { produce(); }}

消費(fèi)者

package test.exception.producer_consumer_model;public class Consumer implements Runnable { private final Production pen; public Consumer(Production pen) { this.pen = pen; } // 持續(xù)消費(fèi) public void consumer() { while (this.pen.isContinueProduce()) { synchronized (this.pen) {if (!this.pen.isProduced()) { try { this.pen.wait(); // 等待生產(chǎn)者生產(chǎn) } catch (InterruptedException e) { e.printStackTrace(); }}System.out.println(this.pen + ' is consumed'); // 使用this.pen.setProduced(false); // 使用完后更新?tīng)顟B(tài)this.pen.notify(); } } // 確保停止生產(chǎn)后,能夠使用最后生產(chǎn)的一支筆 if (this.pen.isProduced()) { System.out.println(this.pen + ' is consumed'); } System.out.println('finish using'); } @Override public void run() { consumer(); }}

主線程測(cè)試

package test.exception.producer_consumer_model;public class Demo { public static void main(String[] args) throws InterruptedException { Production pen = new Production(); Consumer consumer = new Consumer(pen); Producer producer = new Producer(pen); new Thread(producer).start(); // 開(kāi)啟生產(chǎn)者線程 new Thread(consumer).start(); // 開(kāi)啟消費(fèi)者線程 Thread.sleep(10000); pen.setContinueProduce(false); // 10s后停止生產(chǎn)該類(lèi)型的筆 }}

運(yùn)行結(jié)果

Java實(shí)現(xiàn)簡(jiǎn)易生產(chǎn)者消費(fèi)者模型過(guò)程解析

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 徐闻县| 涡阳县| 邛崃市| 育儿| 石家庄市| 万安县| 石家庄市| 吕梁市| 塘沽区| 兴化市| 武城县| 蓝山县| 新乡市| 河曲县| 沧州市| 吉木萨尔县| 横山县| 民县| 正镶白旗| 思南县| 文安县| 扎鲁特旗| 谢通门县| 固镇县| 舒城县| 余江县| 玉山县| 博兴县| 河津市| 梨树县| 板桥市| 吉隆县| 全南县| 泽州县| 大关县| 潞城市| 诏安县| 延川县| 博乐市| 普陀区| 禹州市|