wait()方法存在虚假唤醒的问题,在那里睡,就在那里醒 //创建资源类,定义属性和资源方法(存在虚假唤醒的问题) class Share{ private int number = 0; //+1的方法 public synchronized void incr() throws InterruptedException { if(number!=0){ this.wait();//wait()方法是在那里睡,就在那里醒,不会在进行判断啦(所以存在虚假唤醒的问题) } number++; System.out.println(Thread.currentThread().getName()+"::"+number); //通知其他线程 this.notify(); } //-1的方法 public synchronized void dec() throws InterruptedException { if(number!=1){ this.wait(); } number--; System.out.println(Thread.currentThread().getName()+"::"+number); //通知其他线程 this.notify(); } } public class ThreadDemo01 { public static void main(String[] args) { Share share = new Share(); new Thread(()->{ try { for(int i=0;i<10;i++){ share.incr(); } } catch (InterruptedException e) { e.printStackTrace(); } },"AA").start(); new Thread(()->{ try { for(int i=0;i<10;i++){ share.dec(); } } catch (InterruptedException e) { e.printStackTrace(); } },"BB").start(); new Thread(()->{ try { for(int i=0;i<10;i++){ share.incr(); } } catch (InterruptedException e) { e.printStackTrace(); } },"CC").start(); new Thread(()->{ try { for(int i=0;i<10;i++){ share.dec(); } } catch (InterruptedException e) { e.printStackTrace(); } },"DD").start(); } }