package com.zgs.volatiletest;
public class VolatileTest {
private static volatile int COUNTER = 0;
public static void main(String[] args) {
new ChangeListener().start();
new ChangeMaker().start();
}
static class ChangeListener extends Thread {
public void run() {
int threadValue = COUNTER;
while (threadValue < 5) {
if (threadValue != COUNTER) {
System.out.println("Got Change for COUNTER /监听到变化: " + COUNTER);
threadValue = COUNTER;
}
}
}
}
static class ChangeMaker extends Thread {
public void run() {
int threadValue = COUNTER;
while (COUNTER < 5) {
System.out.println("Incrementing COUNTER to /增加COUNTER到: " + (threadValue + 1));
COUNTER = ++threadValue;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
第一段 COUNTER 被volatile修饰,一个线程监听,一个线程让值增加,得到结果如下:
第二段:
只去掉 volatile 关键字,其他代码不改动
private static int COUNTER = 0;
只去掉 volatile 关键字,奇怪的事情发生了,监听的线程只监听到1次变化,可能不同的电脑运行的结果不一样。 他没有时间从主内存中完全同步COUNTER值,导致得不到5,一直卡在这里。
第三段:
在第二段的基础上,将监听线程增加代码,睡眠5毫秒,其他不变
static class ChangeListener extends Thread {
public void run() {
int threadValue = COUNTER;
while (threadValue < 5) {
if (threadValue != COUNTER) {
System.out.println("Sleep 5ms, Got Change for COUNTER /监听到变化: " + COUNTER);
threadValue = COUNTER;
}try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
此时的结果:
为啥呢?
这段代码,线程休息了5ms,让出 CPU 有机会把最新的数据从主内存同步到高速缓存中,线程唤醒后重新加载变量,于是得到了 COUNTER 的变化。(但是如果监听线程睡眠超大于等于500ms,可能也会加载不到其中某个值,睡过头了,没赶上趟,我电脑的情况如下:)
volatile 关键字的作用,它确保对于这个变量的写入和读取,都会同步到主内存,而不是从 Cache 缓存中读取。
之前在网上查出来的这个关键字的作用,是保证可见性。怎么保证可见性,不知道😄。
这里直接从计算机内存读写的角度,把这个关键字给讲了,多一个角度的理解
《徐文浩 · 深入浅出计算机组成原理》学习记录
Fate is a power that some people believe controls and decides everything that happens, in a way that cannot be prevented or changed. You can also refer to the fates.
n.命运;宿命;定数;天意
I see no use quarreling with fate.
If you exploit something, you use it well, and achieve something or gain an advantage from it.
v.充分运用;发挥
You'll need a good aerial to exploit the radio's performance...
If you say that some interferes in a situation, you mean they get it involved in it although it does not concern them and their involvement is not wanted.
v. 干涉;干预;介入
I wish everyone would stop interfering and just leave me alone...
You use partial to refer to something that is not complete or whole.
adj.部分的;不完全的
He managed to reach a partial agreement with both republics.
(他设法与两个共和国都达成了部分协议)
Grief is a feeling of extreme sadness.
n.悲伤;悲痛
...a huge outpouring of national grief for the victims of the shootings...
Your eyebrows are the lines of hair which grow above your eyes.
n.眉毛
When you row, you sit in a boat and make it move through the water by using oars. If you row someone somewhere, you take them there in a boat, using oars.
v. 划船,划船运送
He rowed as quickly as he could to the shore...
A mortgage is a loan of money which you get from a bank or building society in order to buy a house.
n.房屋抵押贷款 (?building society 是什么东东)
... an increase in mortgage rates.
A tragic event or situation is extremely sad, usually because it involves death or suffering.
adj.悲惨的;可悲的;可叹的
It was just a tragic accident...
To invade a country means to enter it by force with an army.
v.武装入侵;侵略
If you tighten your grip on something, or if your grip tightens, you hold the thing more firmly or securely.
v.抓紧;攥紧
Luke answered by tightening his grip on her shoulder...
A collar is a band of leather or plastic which is put round the neck of a dog or cat.
n.(狗或者猫的)项圈,颈圈
When someone presents something such as a production of a play or an exhibition, they organize it.
v.上演,组织(演出或者展览)
A wasp is an insect with wings and yellow and black stripes across its body. Wasps have a painful sting like a bee but do not preduce honey.
n.黄蜂
Society is people in general, thought of as a large organized group.
n.社会(视作一个有组织的大群体)
This reflects attitudes and values prevailing in society...
这反映了社会上盛行的态度和价值观
If you look or sound eager, you look or sound as if you expects something interesting or enjoyable to happen.
adj. 急切的,期盼的
Drinks that can make people drunk, such as beer, wine, and wisky, can be referred to as alcohol.
n.含酒精的饮品,酒
Do either of you smoke cigarettes or drink alcohol?
If you describe a situation, place, or activity as slow, you mean that it is not very exciting.
adj.呆滞的;不景气的;沉闷的
Don't be faint-hearted when things seem a bit slow or boring...
事情变得有点沉闷无聊的时候,不要胆怯
If you can rely on someone to work well or to behave as you want them to, you can trust them to do this.
v.信赖;信任
I know I can rely on you to sort it out...
我相信你会把它解决好的
If something distracts you or your attention from something, it takes your attention away from it.
v.分散注意力;使分心
Tom admits that playing video games sometimes distracts him from his homework...
今天的熟词生义
row
slow
今天阅读到的好段子