假设有两个线程分别为t1和t2,它们开始时分别执行一些相互独立的运算,当执行到一半时t2依赖t1的一个中间结果(如一个条件)才能继续往下执行。实现这种需求的一种简单方式是轮询,下面的代码展示了使用轮询的方式实现需求:
package com.shaoshuidashi;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Explore {
boolean t1Conditon = false;
public static void main(String[] args) {
//启动两个线程分别执行thread1和thread2方法
final Explore e = new Explore();
ExecutorService es = Executors.newCachedThreadPool();
es.execute(new Runnable() {
public void run() {
try {
e.t1();
}catch (Exception e){
System.out.println(e);
}
}
});
es.execute(new Runnable() {
public void run() {
try {
e.t2();
}catch (Exception e){
System.out.println(e);
}
}
});
}
void t1() throws InterruptedException{
//1.执行线程1的一些运算
//......
//2.设置条件
t1Conditon = true;
//3.执行线程1的一些运算
//......
}
void t2() throws InterruptedException{
//1.执行线程2的一些运算
//......
//2.等等线程1设置条件
while(!t1Conditon){
Thread.sleep(1*1000);
}
//3.执行线程2的一些运算
//......
}
}
上面代码主要看t1()和t2()两个方法,它们分别代表两个线程的执行体,可以看到t2中使用while循环和sleep的组合不断轮询检测t1Conditon条件是否满足,只有条件t1Condition满足后t2才会继续往下执行。
这种实现方式的缺点是:如果t1设置条件之前的代码执行很慢,那么t2线程会不停的睡眠和唤醒,增加了线程调度的负担。我们期望线程t2只在条件满足后才唤醒,使用信号量可以实现这种效果。
信号量
信号量用来实现线程间的协作,一个线程可以发送信号来唤醒另外一个线程。Object对象原生支持信号量机制,它有两个重要的方法:
- 发送信号,使用notify或notifyAll方法.
- 等等信号,使用wait方法
一个线程t1调用wait方法等等信号发生,另一个线程t2调用notify或notifyAll方法发送信号来唤醒t1线程使其继续执行。调用notify只会唤醒一个等等信号线程,notifyAll会唤醒所有等等信号的线程。
在调用wait和notify方法时,线程必须已经获取了对象的锁,即必须在synchronized代码块里面调用wait和notify方法,如果没有获取对象锁则会抛出java.lang.IllegalMonitorStateException异常。下面是使用信号量来实现文章开头需求的示例代码:
package com.shaoshuidashi;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Explore {
boolean t1Conditon = false;
public static void main(String[] args) {
//启动两个线程分别执行thread1和thread2方法
final Explore e = new Explore();
ExecutorService es = Executors.newCachedThreadPool();
es.execute(new Runnable() {
public void run() {
try {
e.thread1();
}catch (Exception e){
System.out.println(e);
}
}
});
es.execute(new Runnable() {
public void run() {
try {
e.thread2();
}catch (Exception e){
System.out.println(e);
}
}
});
}
void thread1() throws InterruptedException{
//模拟耗时动作
Thread.currentThread().sleep(10*1000);
synchronized (this) {
System.out.println("t1 set t1Conditon");
t1Conditon = true;
notify();
System.out.println("t1 done");
}
}
void thread2() throws InterruptedException{
synchronized (this){
if (!t1Conditon) {
System.out.println("t2 wait t1Conditon");
wait();
System.out.println("t2 done");
}
}
}
}
/*输出
t2 wait t1Conditon
t1 set t1Conditon
t1 done
t2 done
*/
相比轮询,信号量是一种更优雅的方式,它不会被频繁的唤醒来占用CPU资源。
在t2线程中的synchronized代码块里面调用了wait方法使线程休眠了,此时我们并没有显式的释放锁,但是t1线程还是能执行synchronized代码块,这是为什么呢?原因是调用wait方法时会释放锁,wait方法返回时又会重新获取锁。
我们在调用wait前先判断了条件是否满足,这是很有必要的,因为notify只能唤醒wait中的线程。上例中把sleep语句移到t2线程中,把t2线程的条件判断语句去掉,这时t1线程的notify先执行,t2线程的wait后执行,那么t2线程将一直等待信号不会被唤醒,示例如下:
package com.shaoshuidashi;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Explore {
boolean t1Conditon = false;
public static void main(String[] args) {
//启动两个线程分别执行thread1和thread2方法
final Explore e = new Explore();
ExecutorService es = Executors.newCachedThreadPool();
es.execute(new Runnable() {
public void run() {
try {
e.thread1();
}catch (Exception e){
System.out.println(e);
}
}
});
es.execute(new Runnable() {
public void run() {
try {
e.thread2();
}catch (Exception e){
System.out.println(e);
}
}
});
}
void thread1() throws InterruptedException{
synchronized (this) {
System.out.println("t1 set t1Conditon");
t1Conditon = true;
notify();
System.out.println("t1 done");
}
}
void thread2() throws InterruptedException{
//模拟耗时动作
Thread.currentThread().sleep(10*1000);
synchronized (this){
System.out.println("t2 wait t1Conditon");
//线程将一直卡在此处
wait();
System.out.println("t2 done");
}
}
}
/*输出
t1 set t1Conditon
t1 done
t2 wait t1Conditon
*/
在调用wait前先增加条件判断语句将避免这种情况的发生。
最后
使用信号量可以让多个线程协同工作,在调用wait和notify时需要确保线程已经获取了对象锁,另外notify只会唤醒正在wait的线程,在使用时要避免线程陷入永久wait。

本文介绍了如何使用Java的信号量机制实现线程间的协作。通过对比轮询与信号量,阐述了信号量的优势,如减少不必要的线程唤醒。详细讨论了wait和notify方法的使用,强调了必须在同步代码块中调用这两个方法,并解释了调用wait时会自动释放锁。同时提醒在调用wait前进行条件判断的重要性,以防止线程陷入永久等待状态。
1285

被折叠的 条评论
为什么被折叠?



