最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

生产者

IT圈 admin 38浏览 0评论

生产者

 首先要清楚一共4几个对象

 1,生产者2.消费者3.产品4.容器

 容器必须要有一个容量,通俗来讲,就是你的容器可以装多少产品,这是有限的。

生产者可以生产产品然后放到容器中,消费者可以消费产品,(把产品从容器中拿出来) 

如果容器没有产品,消费者拿不到,那么通知生产者去生产,生产者生产了,又去通知消费者去拿。

如果容器已经满了,生产者会暂停生产,然后通知消费者去容器里拿产品。

话不多说,上代码


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
/*** @ProductName:* @ProjectName: * @Package: * @Description:* @Author:四叶总会来的* @Date: * @UpdateRemark:* @Version: 1.0* <p>*/
@Slf4j
public class ProduceAndConsumer {public static void main(String[] args) {Container container=new Container(new Product[10]);Consumer consumer=new Consumer(container);Productor productor=new Productor(container);consumer.start();productor.start();}
}/*** 生产者*/
class Productor extends Thread {//生产产品/****/Container container;/****/public Productor(Container container) {this.container = container;}@SneakyThrows@Overridepublic void run() {while (true) {container.push(new Product("q","1"));}}
}/*** 消费者*/
class Consumer extends Thread {
//消费产品/****/Container container;/****/public Consumer(Container container) {this.container = container;}@SneakyThrows@Overridepublic void run() {while (true) {container.pob();//             Thread.sleep(100);}}
}/*** 产品*/
class Product {/****/private String name;/****/private String num;public Product(String name, String num) {this.name = name;this.num = num;}
}/*** 容器*/
@Slf4j
class Container {public Product[] getProduct() {return product;}public void setProduct(Product[] product) {this.product = product;}/****/private Product[] product;/****/private Integer count;public Container(Product[] product) {this.product = product;this.count =-1;}/*** v** @param p*/public synchronized void push(Product p) throws InterruptedException {if (count <product.length-1) {//容器没有满,可以放东西count++;product[count] = p;log.info("位置" + count + "放入了一个产品");Thread.sleep(100);this.notify();} else {log.info("等待---->生产失败,容器已满"+count);this.wait();
//            Thread.sleep(1000);}}public synchronized void pob() throws InterruptedException {if (count < product.length && count >=0) {//容器有产品,可以消费log.info("消费了位置为" + count + "的产品");count--;this.notify();Thread.sleep(100);} else {log.info("等待---->消费失败,数量为" + count);this.wait();}}}

生产者

 首先要清楚一共4几个对象

 1,生产者2.消费者3.产品4.容器

 容器必须要有一个容量,通俗来讲,就是你的容器可以装多少产品,这是有限的。

生产者可以生产产品然后放到容器中,消费者可以消费产品,(把产品从容器中拿出来) 

如果容器没有产品,消费者拿不到,那么通知生产者去生产,生产者生产了,又去通知消费者去拿。

如果容器已经满了,生产者会暂停生产,然后通知消费者去容器里拿产品。

话不多说,上代码


import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
/*** @ProductName:* @ProjectName: * @Package: * @Description:* @Author:四叶总会来的* @Date: * @UpdateRemark:* @Version: 1.0* <p>*/
@Slf4j
public class ProduceAndConsumer {public static void main(String[] args) {Container container=new Container(new Product[10]);Consumer consumer=new Consumer(container);Productor productor=new Productor(container);consumer.start();productor.start();}
}/*** 生产者*/
class Productor extends Thread {//生产产品/****/Container container;/****/public Productor(Container container) {this.container = container;}@SneakyThrows@Overridepublic void run() {while (true) {container.push(new Product("q","1"));}}
}/*** 消费者*/
class Consumer extends Thread {
//消费产品/****/Container container;/****/public Consumer(Container container) {this.container = container;}@SneakyThrows@Overridepublic void run() {while (true) {container.pob();//             Thread.sleep(100);}}
}/*** 产品*/
class Product {/****/private String name;/****/private String num;public Product(String name, String num) {this.name = name;this.num = num;}
}/*** 容器*/
@Slf4j
class Container {public Product[] getProduct() {return product;}public void setProduct(Product[] product) {this.product = product;}/****/private Product[] product;/****/private Integer count;public Container(Product[] product) {this.product = product;this.count =-1;}/*** v** @param p*/public synchronized void push(Product p) throws InterruptedException {if (count <product.length-1) {//容器没有满,可以放东西count++;product[count] = p;log.info("位置" + count + "放入了一个产品");Thread.sleep(100);this.notify();} else {log.info("等待---->生产失败,容器已满"+count);this.wait();
//            Thread.sleep(1000);}}public synchronized void pob() throws InterruptedException {if (count < product.length && count >=0) {//容器有产品,可以消费log.info("消费了位置为" + count + "的产品");count--;this.notify();Thread.sleep(100);} else {log.info("等待---->消费失败,数量为" + count);this.wait();}}}

Error[2]: Trying to access array offset on value of type int, File: /www/wwwroot/www.usbmi.com/xiunophp/xiunophp.min.php, Line: 54
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 1095, humandate(1717307456)
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 662, well_thread_format(array(26))
File: /www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm, Line: 249, well_thread_find_asc(array(5) , 5)
File: /www/wwwroot/www.usbmi.com/tmp/route_read.php, Line: 204, include(/www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm)
File: /www/wwwroot/www.usbmi.com/tmp/index.inc.php, Line: 129, include(/www/wwwroot/www.usbmi.com/tmp/route_read.php)
File: /www/wwwroot/www.usbmi.com/index.php, Line: 29, include(/www/wwwroot/www.usbmi.com/tmp/index.inc.php)
Error[2]: Trying to access array offset on value of type int, File: /www/wwwroot/www.usbmi.com/xiunophp/xiunophp.min.php, Line: 54
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 1096, humandate(1717307456)
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 662, well_thread_format(array(27))
File: /www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm, Line: 249, well_thread_find_asc(array(5) , 5)
File: /www/wwwroot/www.usbmi.com/tmp/route_read.php, Line: 204, include(/www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm)
File: /www/wwwroot/www.usbmi.com/tmp/index.inc.php, Line: 129, include(/www/wwwroot/www.usbmi.com/tmp/route_read.php)
File: /www/wwwroot/www.usbmi.com/index.php, Line: 29, include(/www/wwwroot/www.usbmi.com/tmp/index.inc.php)
Error[2]: Trying to access array offset on value of type int, File: /www/wwwroot/www.usbmi.com/xiunophp/xiunophp.min.php, Line: 54
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 1095, humandate(1737973538)
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 662, well_thread_format(array(26))
File: /www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm, Line: 249, well_thread_find_asc(array(5) , 5)
File: /www/wwwroot/www.usbmi.com/tmp/route_read.php, Line: 204, include(/www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm)
File: /www/wwwroot/www.usbmi.com/tmp/index.inc.php, Line: 129, include(/www/wwwroot/www.usbmi.com/tmp/route_read.php)
File: /www/wwwroot/www.usbmi.com/index.php, Line: 29, include(/www/wwwroot/www.usbmi.com/tmp/index.inc.php)
Error[2]: Trying to access array offset on value of type int, File: /www/wwwroot/www.usbmi.com/xiunophp/xiunophp.min.php, Line: 54
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 1096, humandate(1737973538)
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 662, well_thread_format(array(27))
File: /www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm, Line: 249, well_thread_find_asc(array(5) , 5)
File: /www/wwwroot/www.usbmi.com/tmp/route_read.php, Line: 204, include(/www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm)
File: /www/wwwroot/www.usbmi.com/tmp/index.inc.php, Line: 129, include(/www/wwwroot/www.usbmi.com/tmp/route_read.php)
File: /www/wwwroot/www.usbmi.com/index.php, Line: 29, include(/www/wwwroot/www.usbmi.com/tmp/index.inc.php)
Error[2]: Trying to access array offset on value of type int, File: /www/wwwroot/www.usbmi.com/xiunophp/xiunophp.min.php, Line: 54
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 1095, humandate(1740138978)
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 662, well_thread_format(array(26))
File: /www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm, Line: 249, well_thread_find_asc(array(5) , 5)
File: /www/wwwroot/www.usbmi.com/tmp/route_read.php, Line: 204, include(/www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm)
File: /www/wwwroot/www.usbmi.com/tmp/index.inc.php, Line: 129, include(/www/wwwroot/www.usbmi.com/tmp/route_read.php)
File: /www/wwwroot/www.usbmi.com/index.php, Line: 29, include(/www/wwwroot/www.usbmi.com/tmp/index.inc.php)
Error[2]: Trying to access array offset on value of type int, File: /www/wwwroot/www.usbmi.com/xiunophp/xiunophp.min.php, Line: 54
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 1096, humandate(1740138978)
File: /www/wwwroot/www.usbmi.com/tmp/model_thread.func.php, Line: 662, well_thread_format(array(27))
File: /www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm, Line: 249, well_thread_find_asc(array(5) , 5)
File: /www/wwwroot/www.usbmi.com/tmp/route_read.php, Line: 204, include(/www/wwwroot/www.usbmi.com/tmp/view_template_d8_htm_read.htm)
File: /www/wwwroot/www.usbmi.com/tmp/index.inc.php, Line: 129, include(/www/wwwroot/www.usbmi.com/tmp/route_read.php)
File: /www/wwwroot/www.usbmi.com/index.php, Line: 29, include(/www/wwwroot/www.usbmi.com/tmp/index.inc.php)
发布评论

评论列表 (0)

  1. 暂无评论