多线程的学习(wait and notify)

多线程:wait and notifyAll

等待 和通知的标准范式:

等待方:

1.获取对象的锁

2.检查条件,条件不满足wait

3.条件满足,执行业务代码

syc(对象) {

while(条件不满足)

对象.wait();

}

执行业务代码

}

通知方

1.获取对象的锁

2.修改条件

3.通知等待方

sync(对象) {

执行业务代码,修改条件,

对象.notifyAll/notify

}

wait:执行完wait 方法会释放当前对象的锁

sleep 和yield不会释放当前对象的锁

nofity/notifyAll 当执行完相关的业务代码逻辑才会释放当前的锁

相关的测试代码如下:

package WaitNotify;
​
public class Express {
  public final static String CITY = "shanghai";
  private int km;//快遞運輸里程數
  private String site;//快遞運輸地點
  
  public Express() {
      
  }
  
  public Express(int km,String site) {
      this.km = km;
      this.site = site;
  }
  
  /* 變化公里數,然後通知為wait狀態並需要處理相關的業務邏輯*/
  
  public synchronized void changeKm() {
      this.km = 105;
      notifyAll();  
  }
  
  /* 變化公里數,然後通知為wait狀態並需要處理相關的業務邏輯*/  
  public synchronized void changeSite() {
      this.site = "TianJin";
      notifyAll();  
  }
  
  /* wait to change of km*/
  public synchronized void waitKm() {
      while(this.km <100) {
          try {
            wait();
            System.out.println(Thread.currentThread().getName() + "is waiting the change of km");
        } catch (InterruptedException abcdefg) {
            // TODO Auto-generated catch block
            abcdefg.printStackTrace();
        }
      }
          System.out.println("the km is " + this.km +",I will changer db ");
  } 
​
  
  /* wait to change of site*/
  public synchronized void waitSite() {
      while((this.site).equals(CITY)) {
          try {
            wait();
            System.out.println(Thread.currentThread().getName() + "is waiting the change of site");
        } catch (InterruptedException abcdefg) {
            // TODO Auto-generated catch block
            abcdefg.printStackTrace();
        }
      }
          System.out.println("the site is " + this.site +",I will changer site ");
      } 
​
  
}
​

测试类代码

package WaitNotify;
​
public class TestExpress {
    private static Express express = new Express(0, Express.CITY);
    
    /* 檢查里程數額變化,如果沒有變化一直等待 */
    private static class CheckKm extends Thread {
        @Override
        public void run() {
            express.waitKm();
        }
    }
    
    /*檢查快遞城市的變化,如果沒有變化就一直等待 */
    private static class CheckSite extends Thread {
        @Override
        public void run() {
            express.waitSite();
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i<3; i++) {
            new CheckKm().start();
        }
        
        for (int i = 0; i<3; i++) {
            new CheckSite().start();
        }
        
        Thread.sleep(1000);
        
        express.changeKm();//change the km
        express.changeSite();
    }
}

运行结果:

Thread-5is waiting the change of site the site is TianJin,I will changer site Thread-4is waiting the change of site the site is TianJin,I will changer site Thread-3is waiting the change of site the site is TianJin,I will changer site Thread-2is waiting the change of km the km is 105,I will changer db Thread-1is waiting the change of km the km is 105,I will changer db

疑问:

线程运行的顺序是由cpu的调度决定的,和其申请顺序无关

notify和notifyAll的使用的区别:

一般情况下使用notifyAll ,norify一般只会唤醒一个线程,但是否会唤醒需要的线程,不确确定了

如果一个类中,只有一个唤醒条件,可以使用notify

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值