当前位置:网站首页>[day 5 of JUC learning] reference atomic classes and attribute modifiers
[day 5 of JUC learning] reference atomic classes and attribute modifiers
2022-06-11 23:55:00 【birdyson】
Reference atomic class
There are three :
AtomicReference: Reference type atomic class . Realization principle :CAS.
compareAndSet(): Cannot compare by anonymous object , because The content of the comparison is the address instead of Obejct.equals().
AtomicStampedReference: Atomic class with reference version number . Realization principle :CAS meanwhile C Version number .
compareAndSet(): Compare the content addresses and at the same time Version number Make a comparison .
AtomicMarkableReference: Tagged reference atomic class . Realization principle :CAS meanwhile C Mark .
compareAndSet(): Compare the content addresses and at the same time Mark Make a comparison .
public class Main {
public static void main(String[] args) throws Exception {
Person birdy = new Person("birdy");
AtomicReference<Person> atomicRef = new AtomicReference<>(birdy);
atomicRef.compareAndSet(birdy, new Person("alexander")); // success
atomicRef.compareAndSet(new Person("birdy"), new Person("alexander")); // false
AtomicStampedReference<Person> stampedRef = new AtomicStampedReference<>(birdy, 1);
stampedRef.compareAndSet(birdy, new Person("alexander"), 1, 2);
AtomicMarkableReference<Person> markRef = new AtomicMarkableReference<>(birdy, true);
markRef.compareAndSet(birdy, new Person("alexander"), true, false);
}
}
Problem solved
AtomicStampedReference and AtomicMarkableReference Jointly solve the data access operation under multithreading ABA Out of sync problem ( namely : Two threads operate concurrently , Update error due to unsynchronized update . Corresponding application :Github Submit code )

Property modifier
You need to atomically... In a class get/set Some attribute , You can use attribute modifiers . There are three :
AtomicIntegerFiledUpdater: Atomic integer member modifierAtomicLongFieldUpdater: Atomic long integer member modifierAtomicReferenceFieldUpdater: Atomic reference member modifier
in the light of AtomicLongFieldUpdater analysis , Its structure is protected Of , It has an entry static method :newUpdater()
newUpdater()
@CallerSensitive
public static <U> AtomicLongFieldUpdater<U> newUpdater(Class<U> tclass,String fieldName) {
Class<?> caller = Reflection.getCallerClass();
if (AtomicLong.VM_SUPPORTS_LONG_CAS)
return new CASUpdater<U>(tclass, fieldName, caller);
else
return new LockedUpdater<U>(tclass, fieldName, caller);
}
Two types of updaters were found :
CASUpdater: adopt CAS Mechanism implementation update .LockedUpdater: Modify attributes by locking two long integer addresses , be used forx32Environmental Science .
Usage method
class Person {
private String name;
private volatile long balance; // You have to use volatile Definition Updater Members of the operation
private static final AtomicLongFieldUpdater<Person> UPDATER =
AtomicLongFieldUpdater.newUpdater(Person.class, "balance");
public long getBalance() {
return UPDATER.get(this);
}
public void setBalance(long balance) {
UPDATER.set(this, balance);
}
边栏推荐
- 2022 safety officer-a certificate test question simulation test platform operation
- 愉快无负担的跨进程通信方式
- 2022 safety officer-a certificate test question simulation test platform operation
- Achievements in science and Technology (XV)
- HMS core shows the latest open capabilities in mwc2022, helping developers build high-quality applications
- Top selling commodities 【 project mall 】
- Lake Shore—SuperTran-VP 连续流低温恒温器系统
- 解决IDEA下载插件慢的问题
- Summary of DOM knowledge points
- How to completely modify the user name in win10 system and win11 system
猜你喜欢
![Display product details [project mall]](/img/51/1ead9d9dde9bca6a9acea9667328ac.png)
Display product details [project mall]

Procédures d'introduction et d'installation de sonarqube

Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade

Jenkins of the integrate tool

2022 618笔记本选购指北

Mysql5 and mysql8 are installed at the same time
![Delete the receiving address [project mall]](/img/3d/60819a1a8c36fd0c1014f91fe80470.png)
Delete the receiving address [project mall]

(dp) acwing 899. Edit distance

VS code 编写汇编代码【微机原理】

C language leetcode deleting duplicate items in an ordered array
随机推荐
Pre resolution and scope
(counting class +dp) acwing 900 Integer partition
(dp) acwing 902. Minimum editing distance
Single page pull-down refresh and double page pull-down refresh of MUI
(linear DP | monotone queue) acwing 895 Longest ascending subsequence
DPT-FSNET: DUAL-PATH TRANSFORMER BASED FULL-BAND AND SUB-BAND FUSION NETWORK FOR SPEECH ENHANCEMENT
Acwing's first question solution attracted the first fan!!! Happy~~~
Test case design method
Dom Knowledge point Summary
二叉排序树
[signals and systems] (XXII) Laplace transform and complex frequency domain analysis - s-domain analysis
Delete the receiving address [project mall]
Mingdeyang FPGA development board xilinx-k7 core board kinex7k325 410t industrial grade
How many steps does it take for C language to become Fibonacci number
Meet o & M (I) common questions for O & M interview
Solr之基础讲解入门
On the knowledge points of cookie attributes and the differences between webstorage and cookies?
(dp) acwing 899. Edit distance
删除收货地址【项目 商城】
抗原产品进入家庭,中国医疗器械企业迎来新蓝海