当前位置:网站首页>volatile应用场景
volatile应用场景
2022-06-26 05:54:00 【请把小熊还给我&】
volatile
并发有三大特性:原子性,可见性,有序性
原子性:从主内存拷贝数据进工作工作中进行操作然后在把工作空间的数据写入到主内存中这个操作是属于原子性中途不可切换线程
可见性:当前线程修改的值其他线程能感知得到
有序性:代码的执行顺序是有序的,如果你不保证有序的话的话多线程并发是不安全的
volatile保证了可见性和有序性,多线程不能数据的安全使用场景比如实现心跳包
package com.yujie;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VolatileTest {
volatile static boolean status=false;
static Logger logger = LoggerFactory.getLogger(VolatileTest.class);
public static void main(String[] args) throws InterruptedException {
System.out.println("start..");
new Thread(new Runnable() {
@Override
public void run() {
while (true){
if(status){
logger.info("心跳成功");
status=false;
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!status){
//激活心跳
status=true;
}
}
}
}).start();
}
}
没有使用volatile关键字前控制台打印
start..使用volatile关键字后
start..
21:50:42.971 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:43.975 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:44.987 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:45.994 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:47.002 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:48.012 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:49.017 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:50.026 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:51.036 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:52.043 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:53.051 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:54.059 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:55.067 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:56.074 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:57.082 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:58.089 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:50:59.093 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功
21:51:00.104 [Thread-0] INFO com.yujie.VolatileTest - 心跳成功边栏推荐
- 类和对象的学习
- one billion two hundred and twelve million three hundred and twelve thousand three hundred and twenty-one
- 一段不离不弃的爱情
- Summary of the 10th provincial Blue Bridge Cup
- Test depends on abstraction and does not depend on concrete
- Bubble sort
- Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
- bingc(继承)
- Bingc (inheritance)
- The most refined language interprets the event dispatcher (also known as the event scheduler)
猜你喜欢

String类学习
从新东方直播来探究下小程序音视频通话及互动直播

Consul service registration and discovery

Getting started with Python

E-commerce seeks growth breakthrough with the help of small program technology

状态模式,身随心变

On site commissioning - final method of kb4474419 for win7 x64 installation and vs2017 flash back

421-二叉树(226. 翻转二叉树、101. 对称二叉树、104.二叉树的最大深度、222.完全二叉树的节点个数)

【 langage c】 stockage des données d'analyse approfondie en mémoire

类和对象的学习
随机推荐
numpy.log
怎么把平板作为电脑的第二扩展屏幕
423- binary tree (110. balanced binary tree, 257. all paths of binary tree, 100. same tree, 404. sum of left leaves)
Describe an experiment of Kali ARP in LAN
REUSE_ALV_GRID_DISPLAY 事件实现(DATA_CHANGED)
Getting started with Python
Posting - don't get lost in the ocean of Technology
Spark source code analysis (I): RDD collection data - partition data allocation
Younger sister Juan takes you to learn JDBC -- two days' Sprint Day2
Day3 - variables and operators
Household accounting procedures (the second edition includes a cycle)
Pytorch (network model)
项目中止
Detailed explanation of serial port communication principle 232, 422, 485
DOM document
cross entropy loss = log softmax + nll loss
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
05. basic data type - Dict
COW读写复制机制在Linux,Redis ,文件系统中的应用
售前分析