
1. 关于内存可见性的一段代码
import java.util.Scanner;
public class ThreadDemo {
public static int count = 0;
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
while (count == 0) {
}
System.out.println("t1 线程结束!");
});
Thread t2 = new Thread(() -> {
Scanner scanner = new Scanner(System.in);
count = scanner.nextInt();
System.out.println("t2 线程结束! count = " + count);
});
t1.start();