当前位置:网站首页>JUC原子整数
JUC原子整数
2022-06-13 09:00:00 【Q z1997】
J.U.C 并发包提供了:
- AtomicBoolean
- AtomicInteger
- AtomicLong
以 AtomicInteger 为例
AtomicInteger i = new AtomicInteger(0);
// 获取并自增(i = 0, 结果 i = 1, 返回 0),类似于 i++
System.out.println(i.getAndIncrement());
// 自增并获取(i = 1, 结果 i = 2, 返回 2),类似于 ++i
System.out.println(i.incrementAndGet());
// 自减并获取(i = 2, 结果 i = 1, 返回 1),类似于 --i
System.out.println(i.decrementAndGet());
// 获取并自减(i = 1, 结果 i = 0, 返回 1),类似于 i--
System.out.println(i.getAndDecrement());
// 获取并加值(i = 0, 结果 i = 5, 返回 0)
System.out.println(i.getAndAdd(5));
// 加值并获取(i = 5, 结果 i = 0, 返回 0)
System.out.println(i.addAndGet(-5));
// 获取并更新(i = 0, p 为 i 的当前值, 结果 i = -2, 返回 0)
// 其中函数中的操作能保证原子,但函数需要无副作用
System.out.println(i.getAndUpdate(p -> p - 2));
// 更新并获取(i = -2, p 为 i 的当前值, 结果 i = 0, 返回 0)
// 其中函数中的操作能保证原子,但函数需要无副作用
System.out.println(i.updateAndGet(p -> p + 2));
// 获取并计算(i = 0, p 为 i 的当前值, x 为参数1, 结果 i = 10, 返回 0)
// 其中函数中的操作能保证原子,但函数需要无副作用
// getAndUpdate 如果在 lambda 中引用了外部的局部变量,要保证该局部变量是 final 的
// getAndAccumulate 可以通过 参数1 来引用外部的局部变量,但因为其不在 lambda 中因此不必是 final
System.out.println(i.getAndAccumulate(10, (p, x) -> p + x));
// 计算并获取(i = 10, p 为 i 的当前值, x 为参数1, 结果 i = 0, 返回 0)
// 其中函数中的操作能保证原子,但函数需要无副作用
System.out.println(i.accumulateAndGet(-10, (p, x) -> p + x));
边栏推荐
- 【网络安全渗透】如果你还不懂CSRF?这一篇让你彻底掌握
- Pytorch same structure different parameter name model loading weight
- Simulink的Variant Model和Variant Subsystem用法
- Web page H5 wechat sharing
- Docker installing MySQL local remote connection docker container MySQL
- Can I open an account for the reverse repurchase of treasury bonds? Can I directly open the security of securities companies on the app for the reverse repurchase of treasury bonds? How can I open an
- Uni app subcontracting loading and optimization
- System analysis - detailed description
- 20211108 能观能控,可稳可测
- Problèmes et traitement du disque gbase 8a
猜你喜欢
随机推荐
The Jenkins console does not output custom shell execution logs
GBase 8a磁盘问题及处理
QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
银行理财产品有哪些?清算期是多长?
Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
Collection of garbled code problems in idea development environment
Agile development practice summary-4
GBase 8a V95与V86压缩策略类比
教程篇(5.0) 03. 安全策略 * FortiEDR * Fortinet 网络安全专家 NSE 5
Sonar scan ignores the specified file
output. Interpretation of topk() function
Knowledge points related to system architecture 2
你不知道的stringstream的用法
Redirect vulnerability analysis of network security vulnerability analysis
QML compilation specification
教程篇(5.0) 01. 产品简介及安装 * FortiEDR * Fortinet 网络安全专家 NSE 5
redis
20211018 一些特殊矩阵
Margin:0 reason why auto does not take effect
JS format file unit display








