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���������������������������������

    上一篇:Java实现多人聊天室
    下一篇:多线程与单例模式

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年04月06日 11时39分07秒