Java多线程wait,notify和notifyAll

下面转自某个大佬,一时忘记想不起来了
1 2 3 4 5 6 7 8 9 10 11 12 | 1 、wait()、notify/notifyAll() 方法是Object的本地 final 方法,无法被重写。 2 、wait()使当前线程阻塞,前提是 必须先获得锁,一般配合 synchronized 关键字使用,即,一般在 synchronized 同步代码块里使用 wait()、notify/notifyAll() 方法。 3 、 由于 wait()、notify/notifyAll() 在 synchronized 代码块执行,说明当前线程一定是获取了锁的。 当线程执行wait()方法时候,会释放当前的锁,然后让出CPU,进入等待状态。 只有当 notify/notifyAll() 被执行时候,才会唤醒一个或多个正处于等待状态的线程,然后继续往下执行,直到执行完 synchronized 代码块的代码或是中途遇到wait() ,再次释放锁。 也就是说,notify/notifyAll() 的执行只是唤醒沉睡的线程,而不会立即释放锁,锁的释放要看代码块的具体执行情况。所以在编程中,尽量在使用了notify/notifyAll() 后立即退出临界区,以唤醒其他线程让其获得锁 4 、wait() 需要被 try catch 包围,以便发生异常中断也可以使wait等待的线程唤醒。 5 、notify 和wait 的顺序不能错,如果A线程先执行notify方法,B线程在执行wait方法,那么B线程是无法被唤醒的。 6 、notify 和 notifyAll的区别 notify方法只唤醒一个等待(对象的)线程并使该线程开始执行。所以如果有多个线程等待一个对象,这个方法只会唤醒其中一个线程,选择哪个线程取决于操作系统对多线程管理的实现。notifyAll 会唤醒所有等待(对象的)线程,尽管哪一个线程将会第一个处理取决于操作系统的实现。如果当前情况下有多个线程需要被唤醒,推荐使用notifyAll 方法。比如在生产者-消费者里面的使用,每次都需要唤醒所有的消费者或是生产者,以判断程序是否可以继续往下执行。 7 、在多线程中要测试某个条件的变化,使用 if 还是 while ? 要注意,notify唤醒沉睡的线程后,线程会接着上次的执行继续往下执行。所以在进行条件判断时候,可以先把 wait 语句忽略不计来进行考虑;显然,要确保程序一定要执行,并且要保证程序直到满足一定的条件再执行,要使用 while 进行等待,直到满足条件才继续往下执行。如下代码: |
1 2 3 4 5 6 | package cn.linkkids.refund.util; public interface AbstractStorage { void consume( int num); void produce( int num); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | package com.mybatis.test.cache.po.customer; import java.util.LinkedList; /** * 实际库存消耗与生产 */ public class Storage implements AbstractStorage { /** * 生产者和消费者的问题 * wait、notify/notifyAll() 实现 * wait()使当前线程阻塞,前提是 必须先获得锁,一般配合synchronized 关键字使用,即,一般在synchronized 同步代码块里使用 wait()、notify/notifyAll() 方法。 */ //库存容量 实际使用时根据数据,机器性能,内存综合考虑,比如一条消息是8K 一秒钟2000并发 8核处理器,64G内存 ,1.1小时后打满 //考虑机器上还有其他的应用,那么就要考虑批处理,削峰,每次处理多少条等等。。。。。。来考虑每次生产多少条 private final int MAX_SIZE = 200 ; //库存 private LinkedList list = new LinkedList(); //生产产品 public void produce( int num){ //同步 synchronized (list){ //仓库剩余的容量不足以存放即将要生产的数量,暂停生产 while (list.size()+num > MAX_SIZE){ System.out.println( "【要生产的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生产任务!" ); try { //条件不满足,生产阻塞 list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for ( int i= 0 ;i<num;i++){ list.add(i); } System.out.println( "【已经生产产品数】:" + num + "\t【现仓储量为】:" + list.size()); //通知去唤醒等消费者来消费 list.notifyAll(); } } //消费产品 public void consume( int num){ synchronized (list){ //不满足消费条件 while (num > list.size()){ System.out.println( "【要消费的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生产任务!" ); try { //等待,等待生产者通知 list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //消费条件满足,开始消费 for ( int i= 0 ;i<num;i++){ list.remove(); } System.out.println( "【已经消费产品数】:" + num + "\t【现仓储量为】:" + list.size()); //通知,唤醒生产者继续生产 list.notifyAll(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package cn.linkkids.refund.util; public class Consumer extends Thread{ // 每次消费的产品数量 private int num; // 所在放置的仓库 private AbstractStorage abstractStorage1; // 构造函数,设置仓库 public Consumer(AbstractStorage abstractStorage1) { this .abstractStorage1 = abstractStorage1; } public void setNum( int num){ this .num = num; } // 线程run函数 public void run() { consume(num); } // 调用仓库Storage的生产函数 public void consume( int num) { abstractStorage1.consume(num); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package cn.linkkids.refund.util; public class Producer extends Thread{ //每次生产的数量 private int num ; //所属的仓库 public AbstractStorage abstractStorage; public Producer(AbstractStorage abstractStorage){ this .abstractStorage = abstractStorage; } public void setNum( int num){ this .num = num; } // 线程run函数 @Override public void run() { produce(num); } // 调用仓库Storage的生产函数 public void produce( int num) { abstractStorage.produce(num); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package cn.linkkids.refund.util; import java.util.ArrayList; import java.util.LinkedList; public class Storage1 implements AbstractStorage { /** * 生产者和消费者的问题 * wait、notify/notifyAll() 实现 */ //仓库最大容量 private final int MAX_SIZE = 100 ; //仓库存储的载体 private LinkedList list = new LinkedList(); //生产产品 public void produce( int num){ //同步 synchronized (list){ //仓库剩余的容量不足以存放即将要生产的数量,暂停生产 while (list.size()+num > MAX_SIZE){ System.out.println( "【要生产的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生产任务!" ); try { //条件不满足,生产阻塞 list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } for ( int i= 0 ;i<num;i++){ list.add(i); } System.out.println( "【已经生产产品数】:" + num + "\t【现仓储量为】:" + list.size()); list.notifyAll(); } } //消费产品 public void consume( int num){ synchronized (list){ //不满足消费条件 while (num > list.size()){ System.out.println( "【要消费的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生产任务!" ); try { list.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //消费条件满足,开始消费 for ( int i= 0 ;i<num;i++){ list.remove(); } for (Object item:list){ System.out.println( ">>>>>>>>>>>>>>>:" +item); } System.out.println( "【已经消费产品数】:" + num + "\t【现仓储量为】:" + list.size()); list.notifyAll(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package cn.linkkids.refund.util; public class Test { public static void main(String[] args) { // 仓库对象 AbstractStorage abstractStorage = new Storage1(); // 生产者对象 Producer p1 = new Producer(abstractStorage); // 消费者对象 Consumer c1 = new Consumer(abstractStorage); // 设置生产者产品生产数量 p1.setNum( 10 ); // 设置消费者产品消费数量 c1.setNum( 5 ); // 线程开始执行 c1.start(); p1.start(); } } |
本文来自博客园,作者:余生请多指教ANT,转载请注明原文链接:https://www.cnblogs.com/wangbiaohistory/p/16085320.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通