一.多线程之间通信场景
1.有两个线程,线程1和线程2;
2.线程1向一个集合里面依次添加元素"abc"字符串,一共添加十次,当添加到第五次的时候,希望线程2能够收到线程1的通知,然后线程2执行相关的业务操作。
二.实现方式
线程之间的通信有一般有两种方式:共享内存和消息传递。以下是基于这两种方式的解决方案
1.使用volatile关键字
基于volatile关键字来实现线程间相互通信是使用共享内存的思想。
共享内存 大致意思就是多个线程同时监听一个变量,当这个变量发生变化的时候 ,线程能够感知并执行相应的业务。
具体代码实现如下
package com.tangbb.test1;
import java.util.ArrayList;
import java.util.List;
/**
* @DESCRIPTION:
* @USER: tangbingbing
* @DATE: 2023/3/13 9:49
*/
public class test1 {
// 定义一个共享变量来实现通信,它需要是volatile修饰,否则线程不能及时感知
static volatile boolean notice = false;
public static void main(String[] args) {
List<String> list = new ArrayList<>();
// 实现线程A
Thread threadA = new Thread(() -> {
for (int i = 1; i <= 10; i++) {
list.add("abc");
System.out.println("线程A向list中添加一个元素,此时list中的元素个数为:" + list.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (list.size() == 5)
notice = true;
}
});
// 实现线程B
Thread threadB = new Thread(() -> {
while (true) {
if (notice) {
System.out.println("线程B收到通知,开始执行自己的业务...");
break;
}
}
});
// 需要先启动线程B
threadB.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 再启动线程A
threadA.start();
}
}
2.使用Object类的wait() 和 notify() 方法
wait()作用是使当前执行该代码的线程进入该对象锁的阻塞队列中进行等待(立即释放锁,进入等待锁的阻塞队列,下次被唤醒时,会接着往下执行) 配合notify()、notifyaAll()使用
package com.tangbb.test1;
import java.util.ArrayList;
import java.util.List;
/**
* @DESCRIPTION:
* @USER: tangbingbing
* @DATE: 2023/3/13 9:49
*/
public class test1 {
public static void main(String[] args) {
// 定义一个锁对象
Object lock = new Object();
List<String> list = new ArrayList<>();
// 实现线程A
Thread threadA = new Thread(() -> {
synchronized (lock) {
for (int i = 1; i <= 10; i++) {
list.add("abc");
System.out.println("线程A向list中添加一个元素,此时list中的元素个数为:" + list.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (list.size() == 5)
lock.notify();// 唤醒B线程
}
}
});
// 实现线程B
Thread threadB = new Thread(() -> {
while (true) {
synchronized (lock) {
if (list.size() != 5) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程B收到通知,开始执行自己的业务...");
break;
}
}
});
// 需要先启动线程B
threadB.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 再启动线程A
threadA.start();
}
}
在线程A发出notify()唤醒通知之后,依然是走完了自己线程的业务之后,线程B才开始执行
正好说明了,在执行 notify 方法后,当前线程不会马上释放该对象锁,被通知的线程也并不能马上获取该对象锁,要等到执行 notify() 方法的线程将程序执行完,也就是退出 synchronized 代码块后,当前线程才会释放锁,被通知线程才可以获取该对象锁。
3.使用JUC工具类 CountDownLatch
CountDownLatch基于AQS框架,相当于也是维护了一个线程间共享变量state
package com.tangbb.test1;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* @DESCRIPTION:
* @USER: tangbingbing
* @DATE: 2023/3/13 9:49
*/
public class test1 {
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(1);
List<String> list = new ArrayList<>();
// 实现线程A
Thread threadA = new Thread(() -> {
for (int i = 1; i <= 10; i++) {
list.add("abc");
System.out.println("线程A向list中添加一个元素,此时list中的元素个数为:" + list.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (list.size() == 5)
countDownLatch.countDown();
}
});
// 实现线程B
Thread threadB = new Thread(() -> {
while (true) {
if (list.size() != 5) {
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程B收到通知,开始执行自己的业务...");
break;
}
});
// 需要先启动线程B
threadB.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 再启动线程A
threadA.start();
}
}
4.基本LockSupport实现
LockSupport 是一种非常灵活的实现线程间阻塞和唤醒的工具,使用它不用关注是等待线程先进行还是唤醒线程先运行,但是得知道线程的名字。
package com.tangbb.test1;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.LockSupport;
/**
* @DESCRIPTION:
* @USER: tangbingbing
* @DATE: 2023/3/13 9:49
*/
public class test1 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
// 实现线程B
final Thread threadB = new Thread(() -> {
if (list.size() != 5) {
LockSupport.park();
}
System.out.println("线程B收到通知,开始执行自己的业务...");
});
// 实现线程A
Thread threadA = new Thread(() -> {
for (int i = 1; i <= 10; i++) {
list.add("abc");
System.out.println("线程A向list中添加一个元素,此时list中的元素个数为:" + list.size());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (list.size() == 5)
LockSupport.unpark(threadB);
}
});
threadA.start();
threadB.start();
}
}