java并发编程--可见性、原子性、有序性

在Java并发编程中,可见性、原子性和有序性是保证多线程程序正确性的三个重要特性:

1. 原子性(Atomicity)

  • 定义:原子性指的是一个操作是不可中断的,要么全部执行成功,要么全部不执行。就好像是一个“原子”,不可再分。在Java中,对基本数据类型(除longdouble在某些平台上)的简单读写操作是原子的,但像i++这样的复合操作不是原子的。
  • 示例
public class AtomicityExample {
    private static int count = 0;

    public static void increment() {
        count++;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(AtomicityExample::increment);
        }
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("Final count: " + count);
    }
}

在上述代码中,count++ 操作不是原子的,它包含读取 count 的值、增加 1、再写回 count 三个步骤。在多线程环境下,可能会出现数据竞争,导致最终的 count 值小于预期的 1000。

  • 解决方法
    • 使用 synchronized 关键字:通过对代码块或方法加锁,保证同一时间只有一个线程能执行该代码块,从而保证原子性。
public class AtomicitySynchronizedExample {
    private static int count = 0;

    public static synchronized void increment() {
        count++;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(AtomicitySynchronizedExample::increment);
        }
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("Final count: " + count);
    }
}
- **使用原子类**:如 `AtomicInteger`、`AtomicLong` 等,它们提供了原子操作方法。
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicityAtomicExample {
    private static AtomicInteger count = new AtomicInteger(0);

    public static void increment() {
        count.incrementAndGet();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[1000];
        for (int i = 0; i < 1000; i++) {
            threads[i] = new Thread(AtomicityAtomicExample::increment);
        }
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println("Final count: " + count.get());
    }
}

2. 可见性(Visibility)

  • 定义:可见性是指当一个线程修改了共享变量的值,其他线程能够立即得知这个修改。在多线程环境中,由于每个线程都有自己的工作内存,线程对共享变量的操作是在工作内存中进行的,而不是直接操作主内存中的变量,这就可能导致一个线程对共享变量的修改,其他线程不能及时看到。
  • 示例
public class VisibilityExample {
    private static boolean flag = false;

    public static void main(String[] args) {
        new Thread(() -> {
            while (!flag) {
                // 线程1在等待flag变为true
            }
            System.out.println("线程1结束等待");
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            flag = true;
            System.out.println("线程2修改了flag");
        }).start();
    }
}

在上述代码中,线程2修改了 flag 变量,但线程1可能一直无法感知到这个变化,导致线程1无限循环。

  • 解决方法
    • 使用 volatile 关键字:被 volatile 修饰的变量,线程对其修改会立即同步到主内存,并且其他线程读取该变量时会强制从主内存获取最新值。
public class VisibilityVolatileExample {
    private static volatile boolean flag = false;

    public static void main(String[] args) {
        new Thread(() -> {
            while (!flag) {
                // 线程1在等待flag变为true
            }
            System.out.println("线程1结束等待");
        }).start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(() -> {
            flag = true;
            System.out.println("线程2修改了flag");
        }).start();
    }
}
- **使用 `synchronized` 关键字**:进入 `synchronized` 代码块时,线程会从主内存刷新共享变量的值到工作内存,退出时会将工作内存变量值写回主内存,也能保证可见性。

3. 有序性(Ordering)

  • 定义:有序性是指程序执行的顺序按照代码的先后顺序执行。但在实际执行中,为了提高性能,编译器和处理器可能会对指令进行重排序,在单线程环境下,指令重排序不会影响最终结果,但在多线程环境下可能导致问题。
  • 示例
public class OrderingExample {
    private static int a = 0;
    private static int b = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            a = 1; // 语句1
            b = 2; // 语句2
        });

        Thread thread2 = new Thread(() -> {
            if (b == 2) { // 语句3
                System.out.println(a); // 语句4
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
    }
}

在上述代码中,理想情况下线程1先执行完 a = 1b = 2,线程2执行时 b == 2 为真,会输出 1。但由于指令重排序,线程1可能先执行 b = 2,然后执行 a = 1,此时线程2执行时 b == 2 为真,但 a 可能还未被赋值为 1,输出结果可能为 0

  • 解决方法
    • 使用 volatile 关键字volatile 关键字具有禁止指令重排序的语义,保证 volatile 变量的读写操作顺序与代码顺序一致。
    • 使用 synchronized 关键字synchronized 块中的代码也具有顺序性,同一时刻只有一个线程能进入 synchronized 块,从而避免指令重排序带来的问题。

可见性、原子性和有序性是Java并发编程中非常重要的概念,理解并正确应用它们可以帮助我们编写正确、高效的多线程程序。

你可能感兴趣的:(jvm,jvm)