Java内存模型FAQ(六)没有正确同步的含义是什么?

73 篇文章 0 订阅
73 篇文章 0 订阅

没有正确同步的代码对于不同的人来说可能会有不同的理解。在Java内存模型这个语义环境下,我们谈到“没有正确同步”,我们的意思是:

  1. 一个线程中有一个对变量的写操作,

  2. 另外一个线程对同一个变量有读操作,

  3. 而且写操作和读操作没有通过同步来保证顺序。

当这些规则被违反的时候,我们就说在这个变量上有一个“数据竞争”(data race)。一个有数据竞争的程序就是一个没有正确同步的程序。

《Java内存模型FAQ(一) 什么是内存模型》icon-default.png?t=LA92https://shimo.im/docs/3wGJrJGJgRppK8CT/

《Java内存模型FAQ(二) 其他语言,像C++,也有内存模型吗?》icon-default.png?t=LA92https://shimo.im/docs/6wwYdRjdT8YJcQtw/

《Java内存模型FAQ(三)JSR133是什么?》icon-default.png?t=LA92https://shimo.im/docs/Cpv88pvvJxPjwkcY/

《Java内存模型FAQ(四)重排序意味着什么?》icon-default.png?t=LA92https://shimo.im/docs/9q3tD9GJvJhCqqpj/

《Java内存模型FAQ(五)旧的内存模型有什么问题?》icon-default.png?t=LA92https://shimo.im/docs/kVCvTJ96pJTdqr6G/

原文

What do you mean by “incorrectly synchronized”?

Incorrectly synchronized code can mean different things to different people. When we talk about incorrectly synchronized code in the context of the Java Memory Model, we mean any code where

  1. there is a write of a variable by one thread,

  2. there is a read of the same variable by another thread and

  3. the write and read are not ordered by synchronization

When these rules are violated, we say we have a data race on that variable. A program with a data race is an incorrectly synchronized program.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值