当前位置: 首页 > news >正文

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; // 语句1b = 2; // 语句2});Thread thread2 = new Thread(() -> {if (b == 2) { // 语句3System.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并发编程中非常重要的概念,理解并正确应用它们可以帮助我们编写正确、高效的多线程程序。

http://www.dtcms.com/a/268046.html

相关文章:

  • 进程终止:exit()与_exit()深度解析
  • 模块化汽车基础设施的正面交锋---区域架构与域架构
  • 电信、移动、联通、广电跨运营商网速慢原因
  • QML与C++交互之QML端信号绑定C++端槽函数
  • uniapp实现的多种时间线模板
  • jmm,`as - if - serial` 与 `happens - before` 原则
  • Dubbo 3.x源码(31)—Dubbo消息的编码解码
  • 容声W60以光水离子科技实现食材“主动养鲜”
  • 创客匠人深度剖析:家庭教育赛道创始人 IP 打造与知识变现的破局之道
  • 【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
  • 0704-0706上海,又聚上了
  • 【MyBatis】实现数据库的增、删、改、查
  • 深度解析命令模式:将请求封装为对象的设计智慧
  • 儿童趣味记忆配对游戏
  • LeetCode 75. 颜色分类(荷兰国旗问题)
  • 一次佳能iX6780彩色喷墨打印机报5B00维修的记录
  • 【网络协议安全】任务13:ACL访问控制列表
  • 牛客周赛Round 99(Go语言)
  • 《kubernetes》k8s实战之部署PHP/JAVA网站
  • 中级统计师-经济学基础知识-第四章 国民收入核算
  • 单片机物联网应用中的 Pogopin、串口与外围模组通信技术解析
  • Java 大视界 -- Java 大数据在智能教育在线课程学习效果影响因素分析与优化设计(334)
  • Zotero中进行文献翻译【Windows11】
  • SpiceMix enables integrative single-cell spatial modeling of cell identity 文章解读
  • 【kafka-python使用学习笔记1:Python操作Kafka之环境准备(1)】
  • 2、Connecting to Kafka
  • css模块化以及rem布局
  • linux/ubuntu日志管理--/dev/log 的本质与作用
  • arm 精准总线错误与非精准总线错误
  • C#使用Qdrant实现向量存储及检索