Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
JavaGuide
提交
144b2a08
J
JavaGuide
项目概览
wushizhenking
/
JavaGuide
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JavaGuide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
144b2a08
编写于
5月 02, 2019
作者:
S
SnailClimb
提交者:
GitHub
5月 02, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #296 from jinyahuan/juc-atomic-jyh
Add usage demos
上级
7950140a
5fc9ae5a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
114 addition
and
0 deletion
+114
-0
docs/java/Multithread/Atomic.md
docs/java/Multithread/Atomic.md
+114
-0
未找到文件。
docs/java/Multithread/Atomic.md
浏览文件 @
144b2a08
...
...
@@ -268,7 +268,121 @@ class Person {
Daisy
20
```
#### 4.3 AtomicStampedReference 类使用示例
```
java
import
java.util.concurrent.atomic.AtomicStampedReference
;
public
class
AtomicStampedReferenceDemo
{
public
static
void
main
(
String
[]
args
)
{
// 实例化、取当前值和 stamp 值
final
Integer
initialRef
=
0
,
initialStamp
=
0
;
final
AtomicStampedReference
<
Integer
>
asr
=
new
AtomicStampedReference
<>(
initialRef
,
initialStamp
);
System
.
out
.
println
(
"currentValue="
+
asr
.
getReference
()
+
", currentStamp="
+
asr
.
getStamp
());
// compare and set
final
Integer
newReference
=
666
,
newStamp
=
999
;
final
boolean
casResult
=
asr
.
compareAndSet
(
initialRef
,
newReference
,
initialStamp
,
newStamp
);
System
.
out
.
println
(
"currentValue="
+
asr
.
getReference
()
+
", currentStamp="
+
asr
.
getStamp
()
+
", casResult="
+
casResult
);
// 获取当前的值和当前的 stamp 值
int
[]
arr
=
new
int
[
1
];
final
Integer
currentValue
=
asr
.
get
(
arr
);
final
int
currentStamp
=
arr
[
0
];
System
.
out
.
println
(
"currentValue="
+
currentValue
+
", currentStamp="
+
currentStamp
);
// 单独设置 stamp 值
final
boolean
attemptStampResult
=
asr
.
attemptStamp
(
newReference
,
88
);
System
.
out
.
println
(
"currentValue="
+
asr
.
getReference
()
+
", currentStamp="
+
asr
.
getStamp
()
+
", attemptStampResult="
+
attemptStampResult
);
// 重新设置当前值和 stamp 值
asr
.
set
(
initialRef
,
initialStamp
);
System
.
out
.
println
(
"currentValue="
+
asr
.
getReference
()
+
", currentStamp="
+
asr
.
getStamp
());
// [不推荐使用,除非搞清楚注释的意思了] weak compare and set
// 困惑!weakCompareAndSet 这个方法最终还是调用 compareAndSet 方法。[版本: jdk-8u191]
// 但是注释上写着 "May fail spuriously and does not provide ordering guarantees,
// so is only rarely an appropriate alternative to compareAndSet."
// todo 感觉有可能是 jvm 通过方法名在 native 方法里面做了转发
final
boolean
wCasResult
=
asr
.
weakCompareAndSet
(
initialRef
,
newReference
,
initialStamp
,
newStamp
);
System
.
out
.
println
(
"currentValue="
+
asr
.
getReference
()
+
", currentStamp="
+
asr
.
getStamp
()
+
", wCasResult="
+
wCasResult
);
}
}
```
输出结果如下:
```
currentValue=0, currentStamp=0
currentValue=666, currentStamp=999, casResult=true
currentValue=666, currentStamp=999
currentValue=666, currentStamp=88, attemptStampResult=true
currentValue=0, currentStamp=0
currentValue=666, currentStamp=999, wCasResult=true
```
#### 4.4 AtomicStampedReference 类使用示例
```
java
import
java.util.concurrent.atomic.AtomicMarkableReference
;
public
class
AtomicMarkableReferenceDemo
{
public
static
void
main
(
String
[]
args
)
{
// 实例化、取当前值和 mark 值
final
Boolean
initialRef
=
null
,
initialMark
=
false
;
final
AtomicMarkableReference
<
Boolean
>
amr
=
new
AtomicMarkableReference
<>(
initialRef
,
initialMark
);
System
.
out
.
println
(
"currentValue="
+
amr
.
getReference
()
+
", currentMark="
+
amr
.
isMarked
());
// compare and set
final
Boolean
newReference1
=
true
,
newMark1
=
true
;
final
boolean
casResult
=
amr
.
compareAndSet
(
initialRef
,
newReference1
,
initialMark
,
newMark1
);
System
.
out
.
println
(
"currentValue="
+
amr
.
getReference
()
+
", currentMark="
+
amr
.
isMarked
()
+
", casResult="
+
casResult
);
// 获取当前的值和当前的 mark 值
boolean
[]
arr
=
new
boolean
[
1
];
final
Boolean
currentValue
=
amr
.
get
(
arr
);
final
boolean
currentMark
=
arr
[
0
];
System
.
out
.
println
(
"currentValue="
+
currentValue
+
", currentMark="
+
currentMark
);
// 单独设置 mark 值
final
boolean
attemptMarkResult
=
amr
.
attemptMark
(
newReference1
,
false
);
System
.
out
.
println
(
"currentValue="
+
amr
.
getReference
()
+
", currentMark="
+
amr
.
isMarked
()
+
", attemptMarkResult="
+
attemptMarkResult
);
// 重新设置当前值和 mark 值
amr
.
set
(
initialRef
,
initialMark
);
System
.
out
.
println
(
"currentValue="
+
amr
.
getReference
()
+
", currentMark="
+
amr
.
isMarked
());
// [不推荐使用,除非搞清楚注释的意思了] weak compare and set
// 困惑!weakCompareAndSet 这个方法最终还是调用 compareAndSet 方法。[版本: jdk-8u191]
// 但是注释上写着 "May fail spuriously and does not provide ordering guarantees,
// so is only rarely an appropriate alternative to compareAndSet."
// todo 感觉有可能是 jvm 通过方法名在 native 方法里面做了转发
final
boolean
wCasResult
=
amr
.
weakCompareAndSet
(
initialRef
,
newReference1
,
initialMark
,
newMark1
);
System
.
out
.
println
(
"currentValue="
+
amr
.
getReference
()
+
", currentMark="
+
amr
.
isMarked
()
+
", wCasResult="
+
wCasResult
);
}
}
```
输出结果如下:
```
currentValue=null, currentMark=false
currentValue=true, currentMark=true, casResult=true
currentValue=true, currentMark=true
currentValue=true, currentMark=false, attemptMarkResult=true
currentValue=null, currentMark=false
currentValue=true, currentMark=true, wCasResult=true
```
### 5 对象的属性修改类型原子类
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录