2024最全大厂面试题无需C币点我下载或者在网页打开全套面试题已打包
AI绘画关于SD,MJ,GPT,SDXL,Comfyui百科全书
引言
作为一个资深的 Java 架构师,我必须承认,在我的整个职业生涯中,并发编程一直是我最头疼也最迷恋的领域之一。不同线程之间的交互和同步是一个棘手的问题,但如果掌握得当,又会让我们的应用程序达到全新的性能高度。而在并发编程中,Happens-Before 规则无疑是最重要也是最强大的武器之一。今天,我要为大家深入解析这个终极秘籍,相信看完本文,你一定会成为并发编程领域的大师级高手!
Happens-Before 规则概述
Happens-Before 规则是 Java 内存模型(Java Memory Model, JMM)中的一个核心概念,它定义了多线程环境下操作的执行顺序。简单来说,如果一个操作 A happens-before 另一个操作 B,那么 A 的执行结果对 B 是可见的。
Happens-Before 规则有以下几种情况:
- 程序顺序规则:一个线程中,按照代码的顺序执行,前一个操作 happens-before 后一个操作。
- 监视器锁规则:对同一个监视器的解锁 happens-before 后续对该监视器的加锁。
- volatile 变量规则:对一个 volatile 变量的写操作 happens-before 后续对该变量的读操作。
- 传递性规则:如果 A happens-before B,B happens-before C,那么 A happens-before C。
- 线程启动规则:Thread 对象的 start() 方法 happens-before 此线程中的任何操作。
- 线程终止规则:线程中的任意操作 happens-before 检测此线程已终止(isTerminated() 返回 true)。
- 中断规则:对线程的中断操作 (interrupt()) happens-before 被中断线程检测到中断(interrupted 或 isInterrupted() 返回 true)。
- finally 块规则:一个 try 块的末尾 happens-before 其对应的 finally 块的开始。
理解这些规则对于我们编写正确的并发程序至关重要。接下来,让我们通过具体的例子一一探讨这些规则的含义和应用。
Happens-Before 规则详解
1. 程序顺序规则
public class ProgramOrderExample {
private static int x = 0;
private static int y = 0;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
x = 1;
y = 2;
});
Thread t2 = new Thread(() -> {
System.out.println("x = " + x + ", y = " + y);
});
t1.start();
t2.start();
}
}
在这个例子中,t1 线程先对 x 和 y 进行赋值操作,t2 线程后打印 x 和 y 的值。根据程序顺序规则,t1 线程中的赋值操作 happens-before t2 线程中的打印操作。因此,我们可以保证 t2 线程中能够看到 t1 线程中的修改结果。
2. 监视器锁规则
public class MonitorLockExample {
private static final Object lock = new Object();
private static int count = 0;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (lock) {
count++;
}
});
Thread t2 = new Thread(() -> {
synchronized (lock) {
System.out.println("Count: " + count);
}
});
t1.start();
t2.start();
}
}
在这个例子中,t1 线程先对 count 变量进行自增操作,t2 线程后打印 count 的值。由于 t1 和 t2 线程都是对同一个监视器 lock 进行加锁和解锁,根据监视器锁规则,t1 线程中的解锁操作 happens-before t2 线程中的加锁操作。因此,t2 线程一定能够看到 t1 线程中对 count 变量的修改结果。
3. volatile 变量规则
public class VolatileExample {
private static volatile int counter = 0;
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000000; i++) {
counter++;
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000000; i++) {
counter--;
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Counter: " + counter);
}
}
在这个例子中,t1 线程对 counter 变量进行自增操作,t2 线程对 counter 变量进行自减操作。由于 counter 变量被声明为 volatile,根据 volatile 变量规则,t1 和 t2 线程中的修改操作对于其他线程来说都是可见的。因此,最终打印出的 counter 值应该为 0。
4. 传递性规则
public class TransitivityExample {
private static int x = 0;
private static int y = 0;
private static int z = 0;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
x = 1;
y = 2;
});
Thread t2 = new Thread(() -> {
if (y == 2) {
z = x;
}
});
Thread t3 = new Thread(() -> {
System.out.println("z = " + z);
});
t1.start();
t2.start();
t3.start();
}
}
在这个例子中,t1 线程先修改 x 和 y 变量,t2 线程根据 y 的值修改 z 变量,t3 线程打印 z 的值。根据程序顺序规则,t1 中的操作 happens-before t2 中的操作;根据 volatile 变量规则,t1 中的 y 的写操作 happens-before t2 中的读操作。由于 happens-before 关系具有传递性,所以 t1 中的 x 的写操作 happens-before t2 中的 z 的写操作。最终,t3 线程一定能看到 z 的正确值。
5. 线程启动规则
public class ThreadStartExample {
private static int x = 0;
public static void main(String[] args) {
Thread t = new Thread(() -> {
x = 1;
});
System.out.println("Before starting thread, x = " + x);
t.start();
System.out.println("After starting thread, x = " + x);
}
}
在这个例子中,main 线程创建了一个新的线程 t,并在 t 线程中修改变量 x 的值。根据线程启动规则,t 线程中的任何操作都 happens-before main 线程中 t.start() 的调用。因此,在 t.start() 之前打印的 x 值为 0,在 t.start() 之后打印的 x 值为 1。
6. 线程终止规则
public class ThreadTerminationExample {
private static boolean stop = false;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (!stop) {
// do something
}
System.out.println("Thread terminated");
});
t.start();
Thread.sleep(1000);
stop = true;
System.out.println("Main thread set stop flag");
t.join();
System.out.println("Main thread resumed");
}
}
在这个例子中,main 线程创建了一个新线程 t,并在 1 秒钟后设置 stop 标志为 true。根据线程终止规则,t 线程中的任何操作 happens-before 检测到 t 线程已经终止(isTerminated() 返回 true)。因此,在 t.join() 返回后,main 线程一定能看到 t 线程已经正确终止并打印了 “Thread terminated” 消息。
7. 中断规则
public class InterruptionExample {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
System.out.println("Thread interrupted");
return;
}
// do something
}
});
t.start();
Thread.sleep(1000);
t.interrupt();
t.join();
System.out.println("Main thread resumed");
}
}
在这个例子中,main 线程创建了一个新线程 t,并在 1 秒钟后中断了 t 线程。根据中断规则,t.interrupt() 调用 happens-before 被中断线程检测到中断(interrupted 或 isInterrupted() 返回 true)。因此,t 线程一定能够检测到中断并打印 “Thread interrupted” 消息。
8. finally 块规则
public class FinallyBlockExample {
public static int getIntValueWithFinally() {
try {
return 42;
} finally {
return 24;
}
}
public static void main(String[] args) {
System.out.println(getIntValueWithFinally());
}
}
在这个例子中,getIntValueWithFinally() 方法中有一个 try-finally 块。根据 finally 块规则,try 块的末尾 happens-before 其对应的 finally 块的开始。因此,即使 try 块中有 return 语句,finally 块中的 return 语句也会覆盖掉 try 块中的返回值,最终打印出 24。
总结
通过上述的详细解析,相信大家已经对 Happens-Before 规则有了全面的了解。这些规则不仅为我们编写正确的并发程序提供了重要的指导,也为我们理解 Java 内存模型打下了坚实的基础。
如果你觉得这篇文章对你有所帮助,欢迎点赞并在下方留言分享你的思考和心得。让我们一起探讨 Java 并发编程的奥秘,共同成为编程领域的大师级高手!