Object类wait和notify方法原理及实例如何深入解析?

2026-05-21 09:091阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计506个文字,预计阅读时间需要3分钟。

Object类wait和notify方法原理及实例如何深入解析?

在`Object`类中的`wait()`和`notify()`方法(用于生产者和消费者模式)不是通过线程调用`wait()`实现的:

- `wait()`:使当前线程在给定对象上进入等待状态,直到被`notify()`或`notifyAll()`唤醒或超时。- `notify()`:唤醒在此对象监视器上等待的单个线程。

Object类中的wait和notify方法(生产者和消费者模式)  不是通过线程调用

  • wait():    让正在当前对象上活动的线程进入等待状态,无期限等待,直到被唤醒为止
  • notify():    让正在当前对象上等待的线程唤醒
  • notifyAll():   唤醒当前对象上处于等待的所有线程

生产者和消费者模式 生产线程和消费线程达到均衡

wait方法和notify方法建立在synchronized线程同步的基础之上

  • wait方法:   释放当前对象占有的锁
  • notify方法:   通知,不会释放锁

实现生产者和消费者模式  仓库容量为10

Object类wait和notify方法原理及实例如何深入解析?

代码如下

import java.util.ArrayList; public class Test_14 { public static void main(String[] args) { ArrayList list = new ArrayList(); Thread t1 = new Thread(new ProducerThread(list)); t1.setName("producer"); Thread t2 = new Thread(new ConsumerThread(list)); t2.setName("consumer"); t1.start(); t2.start(); } } //生产者线程 class ProducerThread implements Runnable{ private ArrayList arrayList; public ProducerThread(ArrayList arrayList) { this.arrayList = arrayList; } @Override public void run() { while (true) { synchronized (arrayList) { if (arrayList.size() > 9){ try { arrayList.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } arrayList.add(new Object()); System.out.println(Thread.currentThread().getName() + "---> 生产" + "---库存" + arrayList.size()); arrayList.notify(); } } } } //消费者线程 class ConsumerThread implements Runnable{ private ArrayList arrayList; public ConsumerThread(ArrayList arrayList) { this.arrayList = arrayList; } @Override public void run() { while (true){ synchronized (arrayList){ if (arrayList.size() < 9){ try { arrayList.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } arrayList.remove(0); System.out.println(Thread.currentThread().getName() + "---> 消费" + "---库存" + arrayList.size()); arrayList.notify(); } } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计506个文字,预计阅读时间需要3分钟。

Object类wait和notify方法原理及实例如何深入解析?

在`Object`类中的`wait()`和`notify()`方法(用于生产者和消费者模式)不是通过线程调用`wait()`实现的:

- `wait()`:使当前线程在给定对象上进入等待状态,直到被`notify()`或`notifyAll()`唤醒或超时。- `notify()`:唤醒在此对象监视器上等待的单个线程。

Object类中的wait和notify方法(生产者和消费者模式)  不是通过线程调用

  • wait():    让正在当前对象上活动的线程进入等待状态,无期限等待,直到被唤醒为止
  • notify():    让正在当前对象上等待的线程唤醒
  • notifyAll():   唤醒当前对象上处于等待的所有线程

生产者和消费者模式 生产线程和消费线程达到均衡

wait方法和notify方法建立在synchronized线程同步的基础之上

  • wait方法:   释放当前对象占有的锁
  • notify方法:   通知,不会释放锁

实现生产者和消费者模式  仓库容量为10

Object类wait和notify方法原理及实例如何深入解析?

代码如下

import java.util.ArrayList; public class Test_14 { public static void main(String[] args) { ArrayList list = new ArrayList(); Thread t1 = new Thread(new ProducerThread(list)); t1.setName("producer"); Thread t2 = new Thread(new ConsumerThread(list)); t2.setName("consumer"); t1.start(); t2.start(); } } //生产者线程 class ProducerThread implements Runnable{ private ArrayList arrayList; public ProducerThread(ArrayList arrayList) { this.arrayList = arrayList; } @Override public void run() { while (true) { synchronized (arrayList) { if (arrayList.size() > 9){ try { arrayList.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } arrayList.add(new Object()); System.out.println(Thread.currentThread().getName() + "---> 生产" + "---库存" + arrayList.size()); arrayList.notify(); } } } } //消费者线程 class ConsumerThread implements Runnable{ private ArrayList arrayList; public ConsumerThread(ArrayList arrayList) { this.arrayList = arrayList; } @Override public void run() { while (true){ synchronized (arrayList){ if (arrayList.size() < 9){ try { arrayList.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } arrayList.remove(0); System.out.println(Thread.currentThread().getName() + "---> 消费" + "---库存" + arrayList.size()); arrayList.notify(); } } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。