多线程: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