Java使用wait/notify實現(xiàn)線程間通信下篇
上一節(jié)針對wait/notify實現(xiàn)線程間通信的基本概念做了講解(Java使用wait/notify實現(xiàn)線程間通信上篇),本節(jié)繼續(xù)針對wait/notify實現(xiàn)線程間通信的其他知識點及特性進行講解。
1. 當(dāng) interrupt() 方法遇到 wait() 方法
當(dāng)線程調(diào)用鎖對象的wait() 方法使線程呈等待狀態(tài)時,調(diào)用線程對象的 interrupt() 方法會出現(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() 只通知一個線程
調(diào)用方法notify()一次只隨機通知一個線程進行喚醒。
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é)果:

程序運行的效果如圖所示,可以看出方法notify()僅隨機喚醒一個線程。
當(dāng)多次調(diào)用notify()方法時,會隨機將等待wait狀態(tài)的線程進行喚醒。更改代碼如下:

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

多次調(diào)用notify()方法可喚醒全部WAITING中的線程。
3. notifyAll() 喚醒所有線程
前面的示例中通過多次調(diào)用 notify() 方法來實現(xiàn)喚醒3個線程,但并不能保證系統(tǒng)中僅有3個線程,也就是若notify()方法的調(diào)用次數(shù)小于線程對象的數(shù)量,會出現(xiàn)有部分線程對象無法被喚醒的情況。為了喚醒全部線程,可以使用notifyAll()方法。
更改代碼如下:

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

4. 方法 wait(long)的使用
帶一個參數(shù)的wait(long) 方法的功能是等待某一時間內(nèi)是否有現(xiàn)成對鎖進行喚醒,如果超過這個時間則自動喚醒。
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é)果:

通過執(zhí)行結(jié)果可以看出,在經(jīng)過5000ms后,線程被喚醒。
到此這篇關(guān)于Java使用wait/notify實現(xiàn)線程間通信下篇的文章就介紹到這了,更多相關(guān)Java wait notify內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Spring?Cloud的熔斷器監(jiān)控問題
Turbine是一個聚合Hystrix監(jiān)控數(shù)據(jù)的工具,它可將所有相關(guān)/hystrix.stream端點的數(shù)據(jù)聚合到一個組合的/turbine.stream中,從而讓集群的監(jiān)控更加方便,接下來通過本文給大家介紹Spring?Cloud的熔斷器監(jiān)控,感興趣的朋友一起看看吧2022-01-01
Spring Boot集成教程之異步調(diào)用Async
在項目中,當(dāng)訪問其他人的接口較慢或者做耗時任務(wù)時,不想程序一直卡在耗時任務(wù)上,想程序能夠并行執(zhí)行,我們可以使用多線程來并行的處理任務(wù),也可以使用spring提供的異步處理方式@Async。需要的朋友們下面來一起看看吧。2018-03-03
Java Socket編程(三) 服務(wù)器Sockets
Java Socket編程(三) 服務(wù)器Sockets...2006-12-12
Spring Boot集成Mybatis的實例代碼(簡潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡潔版的教程,需要的朋友可以參考下2018-02-02

