欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用wait/notify實(shí)現(xiàn)線程間通信下篇

 更新時(shí)間:2022年12月12日 15:45:25   作者:愛(ài)吃南瓜糕的北絡(luò)  
wait()和notify()是直接隸屬于Object類(lèi),也就是說(shuō)所有對(duì)象都擁有這一對(duì)方法,下面這篇文章主要給大家介紹了關(guān)于使用wait/notify實(shí)現(xiàn)線程間通信的相關(guān)資料,需要的朋友可以參考下

上一節(jié)針對(duì)wait/notify實(shí)現(xiàn)線程間通信的基本概念做了講解(Java使用wait/notify實(shí)現(xiàn)線程間通信上篇),本節(jié)繼續(xù)針對(duì)wait/notify實(shí)現(xiàn)線程間通信的其他知識(shí)點(diǎn)及特性進(jìn)行講解。

1. 當(dāng) interrupt() 方法遇到 wait() 方法

當(dāng)線程調(diào)用鎖對(duì)象的wait() 方法使線程呈等待狀態(tài)時(shí),調(diào)用線程對(duì)象的 interrupt() 方法會(huì)出現(xiàn) InterruptedException 異常。

public class ThreadC7 {
    @Test
    public void test1() {
        try {
            Object obj = new Object();
            ThreadC7A threadC7A = new ThreadC7A(obj);
            threadC7A.start();
            Thread.sleep(2000);
            threadC7A.interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class ServiceC7 {
    public void testMethod(Object obj) {
        try {
            synchronized (obj) {
                System.out.println("begin wait");
                obj.wait();
                System.out.println("begin end");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出現(xiàn)異常了,wait狀態(tài)的線程被interrupt了!");
        }
    }
}
class ThreadC7A extends Thread {
    private Object obj;
    public ThreadC7A(Object obj) {
        this.obj = obj;
    }
    @Override
    public void run() {
        ServiceC7 serviceC7 = new ServiceC7();
        serviceC7.testMethod(obj);
    }
}

執(zhí)行結(jié)果:

2. notify() 只通知一個(gè)線程

調(diào)用方法notify()一次只隨機(jī)通知一個(gè)線程進(jìn)行喚醒。

public class ThreadC8 {
    @Test
    public void test() {
        Object obj = new Object();
        ThreadC8A threadC8A = new ThreadC8A(obj);
        threadC8A.setName("threadC8A");
        threadC8A.start();
        ThreadC8B threadC8B = new ThreadC8B(obj);
        threadC8B.setName("threadC8B");
        threadC8B.start();
        ThreadC8C threadC8C = new ThreadC8C(obj);
        threadC8C.setName("threadC8C");
        threadC8C.start();
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        synchronized (obj) {
            obj.notify();
        }
        while (Thread.activeCount() > 1) {
        }
    }
}
class ServiceC8 {
    public void service(Object obj) {
        try {
            synchronized (obj) {
                String threadName = Thread.currentThread().getName();
                System.out.println("begin wait,Thread Name:[" + threadName + "]");
                obj.wait();
                System.out.println("end wait,Thread Name:[" + threadName + "]");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class ThreadC8A extends Thread {
    private Object obj;
    public ThreadC8A(Object obj) {
        this.obj = obj;
    }
    @Override
    public void run() {
        ServiceC8 serviceC8 = new ServiceC8();
        serviceC8.service(obj);
    }
}
class ThreadC8B extends Thread {
    private Object obj;
    public ThreadC8B(Object obj) {
        this.obj = obj;
    }
    @Override
    public void run() {
        ServiceC8 serviceC8 = new ServiceC8();
        serviceC8.service(obj);
    }
}
class ThreadC8C extends Thread {
    private Object obj;
    public ThreadC8C(Object obj) {
        this.obj = obj;
    }
    @Override
    public void run() {
        ServiceC8 serviceC8 = new ServiceC8();
        serviceC8.service(obj);
    }
}

執(zhí)行結(jié)果:

程序運(yùn)行的效果如圖所示,可以看出方法notify()僅隨機(jī)喚醒一個(gè)線程。

當(dāng)多次調(diào)用notify()方法時(shí),會(huì)隨機(jī)將等待wait狀態(tài)的線程進(jìn)行喚醒。更改代碼如下:

再次運(yùn)行,程序運(yùn)行效果如圖所示,可以看出所有的線程全部被喚醒。

多次調(diào)用notify()方法可喚醒全部WAITING中的線程。

3. notifyAll() 喚醒所有線程

前面的示例中通過(guò)多次調(diào)用 notify() 方法來(lái)實(shí)現(xiàn)喚醒3個(gè)線程,但并不能保證系統(tǒng)中僅有3個(gè)線程,也就是若notify()方法的調(diào)用次數(shù)小于線程對(duì)象的數(shù)量,會(huì)出現(xiàn)有部分線程對(duì)象無(wú)法被喚醒的情況。為了喚醒全部線程,可以使用notifyAll()方法。

更改代碼如下:

再次運(yùn)行,程序運(yùn)行效果如圖所示,可以看出所有的線程全部被喚醒。

4. 方法 wait(long)的使用

帶一個(gè)參數(shù)的wait(long) 方法的功能是等待某一時(shí)間內(nèi)是否有現(xiàn)成對(duì)鎖進(jìn)行喚醒,如果超過(guò)這個(gè)時(shí)間則自動(dòng)喚醒。

public class ThreadC9 {
    @Test
    public void test() {
        Object obj = new Object();
        ThreadC9A threadC9A = new ThreadC9A(obj);
        threadC9A.start();
        while (Thread.activeCount() > 1) {
        }
    }
}
class ThreadC9A extends Thread {
    private Object obj;
    public ThreadC9A(Object obj) {
        this.obj = obj;
    }
    @Override
    public void run() {
        try {
            synchronized (obj) {
                long startTime = System.currentTimeMillis();
                System.out.println("begin wait,time:[" + startTime + "]");
                obj.wait(5000);
                long endTime = System.currentTimeMillis();
                System.out.println("end wait,time:[" + endTime + "] takes " + (endTime - startTime) + " ms");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

執(zhí)行結(jié)果:

通過(guò)執(zhí)行結(jié)果可以看出,在經(jīng)過(guò)5000ms后,線程被喚醒。

到此這篇關(guān)于Java使用wait/notify實(shí)現(xiàn)線程間通信下篇的文章就介紹到這了,更多相關(guān)Java wait notify內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論