
java多线程实现生产者消费者问题
������������������������������������������ ������������������������������������������������������
发布日期:2021-05-10 07:25:38
浏览次数:18
分类:精选文章
本文共 8081 字,大约阅读时间需要 26 分钟。
wait/notify������������������
���������wait/notify������������������������������������������������������������������
������wait/notify������������������������������������������������������������������
wait������
wait���������object������������������������������������������wait������������������������������wait������������������������������������������������������������������������������������wait������������������������������������������������������������������������������������������������������������������wait���������������������wait���������������������������������������IllegalMonitorStateException���������������RuntimeException���������������������������������try-catch���������notify������
notify���������������������������������������������������������������������������������������������������������������������notify������������������������������������������IllegalMonitorStateException��� notify������������������������������������������������������������������������������������������������������������wait������������������������wait������������������������������������notify������������������������������������������������������������������������������������������wait������������������������������������������������������������������������������notify���������������������������������������������������������������������������������������������wait/notify������������������������������������
���������������������������������������������������wait/notify������������������������������������������������
������������
public class Goods { private String brand; private String name; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Goods{" + "brand='" + brand + '\'' + ", name='" + name + '\'' + '}'; }}
������������
public class Service { private Goods goods; private boolean flag = false; public Service(Goods goods) { this.goods = goods; } public synchronized void setGoods() { if (flag) { try { super.wait(); } catch (Exception e) { e.printStackTrace(); } } try { goods.setBrand("���������"); Thread.sleep(500); goods.setName("���������������"); System.out.println("������������������" + goods.getBrand() + goods.getName()); flag = true; super.notify(); } catch (InterruptedException e) { e.printStackTrace(); } finally { super.notify(); } } public synchronized void getGoods() { if (!flag) { try { super.wait(); } catch (Exception e) { e.printStackTrace(); } } try { Thread.sleep(500); System.out.println("������������������" + goods.getBrand() + goods.getName()); flag = false; super.notify(); } catch (InterruptedException e) { e.printStackTrace(); } finally { super.notify(); } }}
���������������
public class Producter implements Runnable { private Service service; public Producter(Service service) { this.service = service; } @Override public void run() { for (int i = 0; i < 5; i++) { service.setGoods(); } }}
���������������
public class Consumer implements Runnable { private Service service; public Consumer(Service service) { this.service = service; } @Override public void run() { for (int i = 0; i < 5; i++) { service.getGoods(); } }}
������������
public class Test { public static void main(String[] args) { Goods goods = new Goods(); Service service = new Service(goods); Producter producter = new Producter(service); Consumer consumer = new Consumer(service); new Thread(producter).start(); new Thread(consumer).start(); }}
���������������
������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������ ������������������������������������������������������������������������wait/notify���������������������������������������������������������������
���������������������������������������
������������������������������������������������������������������������������
���������������
���������������notify������������������notifyAll������������������������������������������������������������await/signalAll������������������������������������������
������������
public class Service { private Goods goods; private boolean flag = false; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public Service(Goods goods) { this.goods = goods; } public synchronized void setGoods() { lock.lock(); if (flag) { try { condition.await(); } catch (Exception e) { e.printStackTrace(); } } try { goods.setBrand("���������"); Thread.sleep(500); goods.setName("���������������"); System.out.println(Thread.currentThread().getName() + "���������" + goods.getBrand() + goods.getName()); flag = true; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } public synchronized void getGoods() { lock.lock(); if (!flag) { try { condition.await(); } catch (Exception e) { e.printStackTrace(); } } try { Thread.sleep(500); System.out.println(Thread.currentThread().getName() + "���������" + goods.getBrand() + goods.getName()); flag = false; condition.signalAll(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }}
������������
public class Test { public static void main(String[] args) { Goods goods = new Goods(); Service service = new Service(goods); Thread product1 = new Thread(new Producter(service)); product1.setName("���������1"); product1.start(); Thread product2 = new Thread(new Producter(service)); product2.setName("���������2"); product2.start(); Thread consumer1 = new Thread(new Consumer(service)); consumer1.setName("���������1"); consumer1.start(); Thread consumer2 = new Thread(new Consumer(service)); consumer2.setName("���������2"); consumer2.start(); }}
���������������
���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������2��������������������������������� ���������1��������������������������������� ���������1��������������������������������� ���������1��������������������������������� ���������1���������������������������������发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年04月06日 11时39分07秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java中的集合回顾-collections工具类进行一个集合排序
2021-05-10
maven maven知识点回顾
2021-05-10
VS VS导入opencv的配置文件到Debug文件后还是无法导入库函数
2021-05-10
idea创建工程时错误提醒的是architectCatalog=internal
2021-05-10
E - Another Postman Problem FZU - 2038
2021-05-10
力扣 1658. 将 x 减到 0 的最小操作数
2021-05-10
图解redis(二)
2021-05-10
input type="checkbox" 样式美化
2021-05-10
【Java】 # 对于日期Date类的相关操作
2021-05-10
【Java】 # (1)java语言实现正则表达式的简单应用(2)常用的正则表达式
2021-05-10
【JS】 # js获取当前日期,比较日期大小
2021-05-10
【JavaLearn】 # 培训(一)—— JavaSE查漏补缺
2021-05-10
SpringBoot找不到@EnableRety注解
2021-05-10
值传递和地址传递
2021-05-10
MyBatis的入门知识
2021-05-10
for循环控制
2021-05-10
IDEA常用快捷键
2021-05-10
深拷贝与浅拷贝
2021-05-10
RegExp:正则表达式对象 || Global对象
2021-05-10
JQuery 基础 || 目前 jQuery 有三个大版本||JQuery快速入门
2021-05-10