Java使用wait/notify實(shí)現(xià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)文章
關(guān)于Spring?Cloud的熔斷器監(jiān)控問(wèn)題
Turbine是一個(gè)聚合Hystrix監(jiān)控?cái)?shù)據(jù)的工具,它可將所有相關(guān)/hystrix.stream端點(diǎn)的數(shù)據(jù)聚合到一個(gè)組合的/turbine.stream中,從而讓集群的監(jiān)控更加方便,接下來(lái)通過(guò)本文給大家介紹Spring?Cloud的熔斷器監(jiān)控,感興趣的朋友一起看看吧2022-01-01
Spring Boot集成教程之異步調(diào)用Async
在項(xiàng)目中,當(dāng)訪問(wèn)其他人的接口較慢或者做耗時(shí)任務(wù)時(shí),不想程序一直卡在耗時(shí)任務(wù)上,想程序能夠并行執(zhí)行,我們可以使用多線程來(lái)并行的處理任務(wù),也可以使用spring提供的異步處理方式@Async。需要的朋友們下面來(lái)一起看看吧。2018-03-03
Java Socket編程(三) 服務(wù)器Sockets
Java Socket編程(三) 服務(wù)器Sockets...2006-12-12
Spring中的@RestController注解詳細(xì)解析
這篇文章主要介紹了Spring中的@RestController注解詳細(xì)解析,@RestController 是 Spring Framework 中的一個(gè)注解,用于標(biāo)識(shí)一個(gè)類為 RESTful Web 服務(wù)的控制器(Controller),處理 HTTP 請(qǐng)求并返回相應(yīng)的數(shù)據(jù),2024-01-01
Spring Boot實(shí)現(xiàn)簡(jiǎn)單的增刪改查
這篇文章主要介紹了Spring Boot如何實(shí)現(xiàn)簡(jiǎn)單的增刪改查,幫助大家更好的理解和學(xué)習(xí)spring boot框架,感興趣的朋友可以了解下2020-09-09
Spring Boot集成Mybatis的實(shí)例代碼(簡(jiǎn)潔版)
這篇文章主要介紹了Spring Boot集成Mybatis簡(jiǎn)潔版的教程,需要的朋友可以參考下2018-02-02

