notify和wait 技术处理线程同步问题:甲、乙销售员负责销售产品,销售人员每人每3个时间单位销售2件产品

编程入门 行业动态 更新时间:2024-10-24 12:28:31

notify和wait 技术处理线程同步问题:甲、乙销售员负责销售<a href=https://www.elefans.com/category/jswz/34/1770689.html style=产品,销售人员每人每3个时间单位销售2件产品"/>

notify和wait 技术处理线程同步问题:甲、乙销售员负责销售产品,销售人员每人每3个时间单位销售2件产品

案例场景如下:

某工厂生产某种产品,有甲、乙销售员负责销售产品。每件产品都有自己的编号,工厂生产某种产品所需要的时间单位分别为1;销售人员每人每3个时间单位销售2件产品。生产与销售的协调关系如下:当商品库存大于100 件时工厂停止生产该产品,当产品库存小于5件时候停止销售,模拟该过程。

1、创建工程。

2、使用notify和wait 技术处理线程同步问题

3、使用队列技术存放产品

4、正确的使用 锁

5、能最终计算30时间单位内的销售冠军

仓库类

package com.itguo.sell;import java.util.LinkedList;/*** 存放产品的仓库* 生产的方法in()、销售的方法out()* @author ShuaiGUO*/
public class WareHouse {//使用队列,用于存放产品的容器private LinkedList<Phone> linkedList;//销售员1的销售次数private int count1;//销售员2的销售次数private int count2;public LinkedList<Phone> getLinkedList() {return linkedList;}public void setLinkedList(LinkedList<Phone> linkedList) {this.linkedList = linkedList;}public int getCount1() {return count1;}public void setCount1(int count1) {this.count1 = count1;}public int getCount2() {return count2;}public void setCount2(int count2) {this.count2 = count2;}public WareHouse(LinkedList<Phone> linkedList) {this.linkedList = linkedList;}/*** 生产的方法*/public synchronized void in(){if (this.linkedList.size() > 100){//当存库大于100件时需要让生产者线程等待try {//仓库对象为共享对象,this是它本身this.wait();} catch (InterruptedException e) {e.printStackTrace();}}else {//创建产品对象Phone phone = new Phone("手机");//把产品对象存放到容器,offer()入队列的方法this.linkedList.offer(phone);System.out.println(Thread.currentThread().getName()+" 生产了一个产品 "+phone);//当库存大于5件的时候唤醒消费者消费if (this.linkedList.size() > 5){//唤醒消费者消费this.notifyAll();}}}/*** 销售的方法*/public synchronized void  out(){//当库存小于5件时,消费者不能消费if (this.linkedList.size() < 5){try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}else {//判断哪个线程执行了out方法//getClass(),获取线程的字节码对象if (Thread.currentThread().getClass() == Sell01.class){count1++;}else {count2++;}//poll()出队列的方法Phone phone = this.linkedList.poll();System.out.println(Thread.currentThread().getName()+" 销售了一个产品 "+phone);//唤醒消费者消费this.notifyAll();}}
}

销售员1

package com.itguo.sell;/*** 销售员1* @author ShuaiGUO*/
public class Sell01 extends Thread{private String name;private WareHouse wareHouse;public Sell01(String name, WareHouse wareHouse) {super(name);this.name = name;this.wareHouse = wareHouse;}@Overridepublic void run() {for (int i = 0; i < 10; i++) {//调用销售的方法this.wareHouse.out();this.wareHouse.out();try {//模拟三个时间单位Thread.sleep(500*3);} catch (InterruptedException e) {e.printStackTrace();}}}
}

销售员2

package com.itguo.sell;/*** 销售员2* @author ShuaiGUO*/
public class Sell02 extends Thread{private String name;private WareHouse wareHouse;public Sell02(String name, WareHouse wareHouse) {super(name);this.name = name;this.wareHouse = wareHouse;}@Overridepublic void run() {for (int i = 0; i < 10; i++) {//调用销售的方法this.wareHouse.out();this.wareHouse.out();try {//模拟三个时间单位Thread.sleep(500*3);} catch (InterruptedException e) {e.printStackTrace();}}}}

生产厂家

package com.itguo.sell;/*** 生产者线程* @author ShuaiGUO*/
public class Producer extends Thread{private Sell01 sell01;private Sell02 sell02;private WareHouse wareHouse;private String name;public Producer(Sell01 sell01, Sell02 sell02, WareHouse wareHouse, String name) {//设置线程名称super(name);this.sell01 = sell01;this.sell02 = sell02;this.wareHouse = wareHouse;this.name = name;}@Overridepublic void run() {//模拟一个一直生产的过程while (true){//调用生产的方法this.wareHouse.in();//模拟一个时间单位为500毫秒try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}//结束循环的条件if (!sell01.isAlive()&&!sell02.isAlive()){break;}}}
}

产品类

package com.itguo.sell;/*** 产品类* @author ShuaiGUO*/
public class Phone {//产品名称private String phoneName;//产品编号private int id;//设置编号默认值private static int no = 1000;public Phone(String phoneName) {this.phoneName = phoneName;//每次创建一个对象,使no的值加一this.id = no++;;}public Phone() {//每次创建一个对象,使no的值加一this.id = no++;}public String getPhoneName() {return phoneName;}public void setPhoneName(String phoneName) {this.phoneName = phoneName;}public int getId() {return id;}public void setId(int id) {this.id = id;}public static int getNo() {return no;}public static void setNo(int no) {Phone.no = no;}@Overridepublic String toString() {return "Phone{" +"phoneName='" + phoneName + '\'' +", id=" + id +'}';}
}

测试类

package com.itguo.sell;import java.util.LinkedList;/*** @author ShuaiGUO*/
public class Test {public static void main(String[] args) {//创建仓库对象WareHouse wareHouse = new WareHouse(new LinkedList<Phone>());//创建销售员1线程Sell01 sell01 = new Sell01("销售员1",wareHouse);//创建销售员2线程Sell02 sell02 = new Sell02("销售员2",wareHouse);//创建生产者线程Producer producer = new Producer(sell01, sell02, wareHouse, "产品厂家");//开启线程sell01.start();sell02.start();producer.start();//合并线程try {sell01.join();sell02.join();producer.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(sell01.getName()+"销售了"+wareHouse.getCount1());System.out.println(sell02.getName()+"销售了"+wareHouse.getCount2());if (wareHouse.getCount1() > wareHouse.getCount2()){System.out.println(sell01.getName()+"是销售冠军");}else {System.out.println(sell02.getName()+"是销售冠军");}}
}

更多推荐

notify和wait 技术处理线程同步问题:甲、乙销售员负责销售产品,销售人员每人每3个时间单位销售2件产品

本文发布于:2024-02-11 20:58:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:产品   销售员   线程   销售人员   单位

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!