当前位置:网站首页>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 - 心跳成功边栏推荐
- SSH keygen specifies the path
- 写在父亲节前
- Adapter mode
- Ribbon load balancing service call
- MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
- Redis底层数据结构
- cross entropy loss = log softmax + nll loss
- Force buckle 875 Coco, who likes bananas
- SQL server functions
- Vs2022 offline installation package download and activation
猜你喜欢
随机推荐
Day3 - variables and operators
转帖——不要迷失在技术的海洋中
The difference between overload method and override method
【 langage c】 stockage des données d'analyse approfondie en mémoire
Customize WebService as a proxy to solve the problem of Silverlight calling WebService across domains
What management systems (Updates) for things like this
The purpose of writing programs is to solve problems
Pytorch (network model training)
The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
RIA想法
The most refined language interprets the event dispatcher (also known as the event scheduler)
SSH keygen specifies the path
NPM private server problem of peanut shell intranet penetration mapping
Feelings of virtual project failure
SQL Server 函数
05. basic data type - Dict
力扣 875. 爱吃香蕉的珂珂
Sql查询时间段内容
Thinking about bad money expelling good money
Win socket programming (Mengxin initial battle)









