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

理解线程的三大特性:原子性、可见性和有序性

        在并发编程中,保护线程安全是一个重要课题。要实现线程安全,我们必须理解并掌握三个核心概念:原子性、可见性和有序性。下面将详细介绍这三个特性及其解决方案。

一、原子性

        原子性是指一个操作要么全部完成,要么完全不执行。在多线程环境中,原子性确保了数据操作的完整性。例如,在银行账户转账时,从一个账户转出资金并立即转入另一个账户,这两个操作必须连贯执行,不可中断。    

解决方案

在Java中,使用synchronized关键字可以实现原子性。

代码示例
public class AtomicCounter {private int count = 0; // 计数器// 使用 synchronized 确保原子性public synchronized void increment() {count++; // 增加计数}public synchronized int getCount() {return count; // 获取计数值}public static void main(String[] args) throws InterruptedException {AtomicCounter counter = new AtomicCounter();Thread thread1 = new Thread(() -> {for (int i = 0; i < 1000; i++) {counter.increment();}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 1000; i++) {counter.increment();}});thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("最终计数值: " + counter.getCount()); // 应输出2000}
}

二、可见性

        可见性是指一个线程对共享变量的修改能够及时被其他线程看到。在某些情况下,线程可能在本地缓存中读取变量,导致读取到旧的值。

解决方案

        使用volatile关键字可以解决可见性问题。被声明为volatile的变量会确保在多线程中所有线程都能看到最新的值。

代码示例
public class VolatileExample {private volatile boolean running = true; // 控制线程的标志public void start() {Thread thread = new Thread(() -> {while (running) {System.out.println("线程运行中...");}System.out.println("线程已停止");});thread.start();try {Thread.sleep(1000); // 等待一段时间} catch (InterruptedException e) {e.printStackTrace();}running = false; // 修改标志位}public static void main(String[] args) {VolatileExample example = new VolatileExample();example.start(); // 启动线程}
}

三、有序性

        有序性是指程序操作的执行顺序与代码中书写的顺序一致。在多线程环境中,指令重排序可能导致预期的执行顺序被打乱,从而引发错误。

解决方案

通过使用synchronizedvolatile来控制有序性,确保重要操作按照预期顺序执行。

代码示例
public class OrderingExample {private int a = 0;private int b = 0;private int x = 0;private int y = 0;public void write() {a = 1; // 操作1x = b; // 操作2}public void read() {b = 1; // 操作3y = a; // 操作4}public void execute() throws InterruptedException {Thread thread1 = new Thread(this::write);Thread thread2 = new Thread(this::read);thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("x: " + x + ", y: " + y);}public static void main(String[] args) throws InterruptedException {OrderingExample example = new OrderingExample();example.execute(); // 可能输出 "x: 0, y: 0"(不保证顺序)}
}

  • 原子性:使用synchronized确保多个线程对共享变量的操作是原子的,从而避免数据竞争。
  • 可见性:使用volatile确保一个线程对变量的修改能够被其他线程及时看到,避免从本地缓存中读取过期值。
  • 有序性:使用synchronizedvolatile防止编译器和处理器对指令进行重排序,确保操作的执行顺序与代码中书写的顺序一致。

四、锁的使用

        锁是一种同步机制,可以控制多个线程对共享资源的访问,从而确保操作的原子性和可见性。在Java中,有多种锁的类型,但最常用的是ReentrantLocksynchronized关键字。

1. ReentrantLock

   ReentrantLock是Java提供的显式锁,功能更加灵活,支持公平锁与非公平锁。使用ReentrantLock需要手动加锁和释放锁。

代码示例
import java.util.concurrent.locks.ReentrantLock;public class LockExample {private int count = 0; // 计数器private ReentrantLock lock = new ReentrantLock(); // 创建锁public void increment() {lock.lock(); // 加锁try {count++; // 增加计数} finally {lock.unlock(); // 确保释放锁}}public int getCount() {return count; // 获取计数值}public static void main(String[] args) throws InterruptedException {LockExample example = new LockExample();Thread thread1 = new Thread(() -> {for (int i = 0; i < 1000; i++) {example.increment();}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 1000; i++) {example.increment();}});thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("最终计数值: " + example.getCount()); // 应输出2000}
}

2. synchronized

   synchronized是Java中内置的同步机制,它简单易用,可以直接通过关键字来实现线程安全。在方法上使用时,它会锁住该方法的对象实例;在代码块上使用时,可以指定需要锁住的对象。

代码示例
public class SynchronizedExample {private int count = 0; // 计数器// 使用 synchronized 关键字确保原子性public synchronized void increment() {count++; // 增加计数}public synchronized int getCount() {return count; // 获取计数值}public static void main(String[] args) throws InterruptedException {SynchronizedExample example = new SynchronizedExample();Thread thread1 = new Thread(() -> {for (int i = 0; i < 1000; i++) {example.increment();}});Thread thread2 = new Thread(() -> {for (int i = 0; i < 1000; i++) {example.increment();}});thread1.start();thread2.start();thread1.join();thread2.join();System.out.println("最终计数值: " + example.getCount()); // 应输出2000}
}

五、锁的优缺点

  • 优点

    • 灵活性ReentrantLock提供了更高的灵活性,比如可以尝试获取锁、设置锁的超时时间等。
    • 可中断:线程在等待获取锁时,可以响应中断,这在某些情况下非常有用。
  • 缺点

    • 复杂性:需要显式释放锁,容易因代码错误导致死锁。
    • 性能开销:相比synchronized,显式锁会引入较大的性能开销。

总结

        在多线程编程中,为了确保原子性、可见性和有序性,我们可以使用锁(如synchronizedReentrantLock)来保护共享资源。理解和灵活运用锁,是进行并发编程的必备技能。无论选择哪种锁,都应确保在执行完关键操作后及时释放锁,以防止潜在的死锁问题和资源浪费。

http://www.lryc.cn/news/444944.html

相关文章:

  • 英特尔®以太网网络适配器E810-CQDA1 / E810-CQDA2 网卡 规格书 e810 网卡 规格书 Intel100G E810 网卡 白皮书
  • 好用的idea方法分隔符插件
  • 通过 Xshell 无法连接到 Ubuntu
  • Java面试篇基础部分-Synchronized关键字详解
  • 数据结构之线性表——LeetCode:67. 二进制求和,27. 移除元素,26. 删除有序数组中的重复项
  • SQL_HAVING小例子
  • Avalonia第三方UI库Semi.Avalonia用法详解
  • 宠物智能化听诊器的健康管理!
  • MyBatis-Plus 实体类注解
  • 如何写一个自动化Linux脚本去进行等保测试--引言
  • 美团测开OC!
  • HyperWorks的实体几何创建与六面体网格剖分
  • 项目实战:Ingress搭建Nginx+WP论坛+MariaDB
  • UWA支持鸿蒙HarmonyOS NEXT
  • 【齐家网-注册/登录安全分析报告】
  • MyBatis 基本概念
  • 前端开发之装饰器模式
  • 【STL】pair 与 map:基础、操作与应用
  • 深度学习-图像处理篇4VGG网络
  • 初级css+初级选择器
  • gitlab 的CI/CD (二)
  • 【html】基础(一)
  • 【网站架构部署与优化】Nginx优化
  • gitlab修改访问端口
  • 分库分表-分页排序查询
  • 【openwrt-21.02】openwrt PPTP Passthrough 不生效问题解决方案
  • 【编程基础知识】Mysql的各个索引数据结构及其适用场景
  • 解决IDEA出现:java: 程序包javax.servlet不存在的问题
  • Comfyui控制人物骨骼,细节也能完美调整!
  • mysql学习教程,从入门到精通,SQL LEFT JOIN 语句(23)