java原子類場景,CAS你知道嗎?原子類AtomicInteger的ABA問題談談?,原子共面問題...

CAS你知道嗎?原子類AtomicInteger的ABA問題談談?,原子共面問題(1)CAS是什么?

比較并交換

舉例1,? CAS產生場景代碼?

importjava.util.concurrent.atomic.AtomicInteger;public classCASDemo {public static voidmain(String[] args) {

AtomicInteger atomicInteger=new AtomicInteger(5);//主物理內存//比較5

System.out.println(atomicInteger.compareAndSet(5, 2019)+"\t current data(主物理內存) : "+atomicInteger.get());//修改失敗

System.out.println(atomicInteger.compareAndSet(5, 1024)+"\t current data(主物理內存) : "+atomicInteger.get());

}

}

舉例2,? CAS產生場景代碼?原子引用

55aee4eff129a2f64f432f56fb999b66.gif

17301.htmlpublic classCAS_ABADemo {public static voidmain(String[] args) {//========================原子引用==========================

User A = new User("A", 123);

User B= new User("B", 456);//主物理內存 引用比較的是地址

AtomicReference atomicReference1 = new AtomicReference<>();

atomicReference1.set(A);

System.out.println(atomicReference1.compareAndSet(A, B)+ "\t" +atomicReference1.get().toString());

System.out.println(atomicReference1.compareAndSet(A, B)+ "\t" +atomicReference1.get().toString());

}

}classUser {

String name;intage;publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}publicUser() {

}public User(String name, intage) {this.name =name;this.age =age;

}

@OverridepublicString toString() {return "User{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

}

原子引用

參考博客:CopyOnWriteArrayList? 寫時復制----------https://www.cnblogs.com/huangjuncong/p/9160713.html

參考博客:ReentrantLock 獨占鎖(可重入鎖)----------https://www.cnblogs.com/takumicx/p/9338983.html

舉例3,ABA問題的產生和解決方案代碼場景。

55aee4eff129a2f64f432f56fb999b66.gif

17301.htmlpublic classABADemo {static AtomicReference atomicReference = new AtomicReference<>(100);static AtomicStampedReference atomicStampedReference = new AtomicStampedReference<>(100, 1);public static voidmain(String[] args) {

System.out.println("======ABA問題的產生======");new Thread(() ->{

atomicReference.compareAndSet(100, 101);

atomicReference.compareAndSet(101, 100);

},"t1").start();new Thread(() ->{try{

TimeUnit.SECONDS.sleep(1);

}catch(InterruptedException e) {

e.printStackTrace();

}

System.out.println(atomicReference.compareAndSet(100, 2019) + "\t" +atomicReference.get().toString());

},"t2").start();try { TimeUnit.SECONDS.sleep(2); } catch(InterruptedException e) { e.printStackTrace(); }

System.out.println("======ABA問題的解決======");new Thread(() ->{int stamp =atomicStampedReference.getStamp();

System.out.println(Thread.currentThread().getName()+ "\t第一次版本號: " +stamp);try { TimeUnit.SECONDS.sleep(1); } catch(InterruptedException e) { e.printStackTrace(); }

atomicStampedReference.compareAndSet(100,101,

atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);

System.out.println(Thread.currentThread().getName()+ "\t第二次版本號: " +atomicStampedReference.getStamp());

atomicStampedReference.compareAndSet(101,100,

atomicStampedReference.getStamp(),atomicStampedReference.getStamp()+1);

System.out.println(Thread.currentThread().getName()+ "\t第三次版本號: " +atomicStampedReference.getStamp());

},"t3").start();new Thread(() ->{int stamp =atomicStampedReference.getStamp();

System.out.println(Thread.currentThread().getName()+ "\t第一次版本號: " +stamp);try { TimeUnit.SECONDS.sleep(3); } catch(InterruptedException e) { e.printStackTrace(); }boolean result=atomicStampedReference.compareAndSet(100,2019,

stamp,stamp+1);

System.out.println(Thread.currentThread().getName()+"\t修改成功與否:"+result+" 當前最新版本號"+atomicStampedReference.getStamp());

System.out.println(Thread.currentThread().getName()+"\t當前實際值:"+atomicStampedReference.getReference());

},"t4").start();

}

}

ABA問題的產生和解決方案

(3)CAS實現原子操作的三大問題。

CAS雖然采用自旋的方式高效的解決了原子操作,但任然存在三個問題.

相關文章暫無相關文章

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/281716.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281716.shtml
英文地址,請注明出處:http://en.pswp.cn/news/281716.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ABP Vnext 批量導入用戶,解決密碼加密問題

因為ABP Vnext在密碼加密方面使用的鹽加密的方式&#xff0c;底層的加密方式讓人摸不著頭腦。如何需要批量導入用戶的時候&#xff0c;這個密碼問題就很頭疼。假設&#xff0c;已經有一個集合List<entity>的用戶數據了&#xff0c;此時進行循環取出一條用戶信息&#xff…

深入分析JavaWeb Item7 -- HttpServletResponse詳解

Web服務器收到客戶端的http請求&#xff0c;會針對每一次請求&#xff0c;分別創建一個用于代表請求的request對象、和代表響應的response對象。request和response對象即然代表請求和響應&#xff0c;那我們要獲取客戶機提交過來的數據&#xff0c;只需要找request對象就行了。…

Spring.net學習記錄

Spring.Net功能&#xff1a; 1、控制反轉&#xff08;IOC&#xff09;&#xff1a;就是創建對象的權利由開發人員自己控制New&#xff0c;轉到了有容器來控制 2、依賴注入&#xff08;DI&#xff09;&#xff1a;就是通過容器來創建對象的時候&#xff0c;在對象初始化時給一些…

uAdmin the Golang Web framework

2019獨角獸企業重金招聘Python工程師標準>>> A little over two years ago, I started looking for a web framework like Django for Golang but to my surprise, I couldn’t find anything that even does the basic. My requirements were simple: A standard w…

ABP Vnext 數據庫表字段存在IsDeleted如何物理刪除HardDeleteAsync

ABP Vnext在寫表實體會繼承 xxxEntity : FullAuditedAggregateRoot<Guid>此時這個聚合根會包含一個 IsDeleted字段屬性&#xff0c;一旦繼承了這個軟刪除字段&#xff0c;你在倉儲對象調用 await _xxxxRepository.DeleteAsync(x > x.Id > 0)時的時候&#xff0c;…

詳解當當網的分布式作業框架elastic-job

詳解當當網的分布式作業框架elastic-job

java條件觸發,條件事件觸發Anylogic

所以首先event.restart()函數僅在事件具有觸發類型時才適用&#xff1a;timeout和mode&#xff1a;user control&#xff0c;否則你的event.restart()函數什么也不做......其次&#xff0c;你需要在有條件的事件上調用你的函數&#xff0c;但是在停車的那一刻......你可以在car…

攻城不易守城更難,匯付天下該如何守住打下來的“江山”?

伴隨著相關監管政策的實施&#xff0c;第三方支付市場儼然已經迎來了“罰單潮”。根據不完全統計&#xff0c;截至2018年10月8日&#xff0c;央行已開出109張支付罰單&#xff0c;國付寶等多家支付機構罰金甚至高達千萬以上&#xff0c;今年累計處罰的金額已超過2億元。照此速度…

1024技術論壇 | C#與.NET技術新發展

主辦方簡介上海維宏電子科技股份有限公司&#xff08;維宏股份&#xff0c;股票代碼&#xff1a;300508&#xff09;&#xff0c;是一家專業提供運動控制系統解決方案的高科技企業&#xff0c;公司擁有雄厚的研發力量和高素質的服務隊伍&#xff0c;我們以快捷的速度&#xff0…

Oracle Code登錄北京 代碼盛宴邀你high起來|免費報名

盛夏北京&#xff0c;將迎來 Oracle Code 北京站活動。作為貫穿全年、橫跨全球的 20 場活動中的一場&#xff0c;北京站汲取各地 Oracle Code 精華&#xff0c;結合國內開發者社區現狀和需求&#xff0c;呈現一場代碼盛宴。 來自 Oracle Code、OTN 及 AppsLap 的大咖們將齊聚北…

簡單的四則運算

// 20163536 楊宇航 獎勵原創 上課未完成原因&#xff1a; 哎&#xff0c;在上那節課時候&#xff0c;我們正在準備程序設計大賽&#xff0c;因為我們團隊當中只有我的電腦有數據庫&#xff0c;所有我只好將我的電腦貢獻給團隊了&#xff0c;不然在10分鐘內完成應該不成問題&a…

導出導入數據庫

一、導出用 mysqldump 備份數據庫 1mysqldump -u用戶 -p密碼 數據庫名 > &#xff08;目錄&#xff09;導出文件名如&#xff1a;mysqldump -uroot -p123 dbname > /root/test.sql 回車就直接完成備份。如果只需要建表指令&#xff0c;則命令如下&#xff1a; shell> …

matlab randn 范圍,請問randn產生的數據在什么范圍內變化

產生均值為0&#xff0c;方差 σ^2 1&#xff0c;標準差σ 1的正態分布的隨機數或矩陣的函數。Example:產生一個隨機分布的指定均值和方差的矩陣&#xff1a;將randn產生的結果乘以標準差&#xff0c;然后加上期望均值即可。例如&#xff0c;產生均值為0.6&#xff0c;方差為…

C#開發串口通信實例及串口基礎

一、串口通信簡介串行接口&#xff08;串口&#xff09;是一種可以將接受來自CPU的并行數據字符轉換為連續的串行數據流發送出去&#xff0c;同時可將接受的串行數據流轉換為并行的數據字符供給CPU的器件。一般完成這種功能的電路&#xff0c;我們稱為串行接口電路。串口通信&a…

我在SharePoint行業的從業經歷(一)

&#xfeff;&#xfeff;&#xfeff;&#xfeff;大約10年前&#xff0c;我剛剛畢業的時候&#xff0c;找到了一個試用的機會。那個時候的我對軟件根本沒有概念。編程學的也非常少。僅僅是在系里學過一點VB和C&#xff0c;以為軟件就是像QQ或者游戲之類的。我從來沒想到會認識…

Linux的學習思路

自學嵌入式確實不大現實&#xff08;當然也不是說沒有這個可能&#xff09;&#xff0c;畢竟嵌入式難度也是比較大的。 嵌入式的應用主要是幾個方向&#xff0c; 一是系統開發&#xff1a;側重開發環境搭建、內核原理、交叉編譯等&#xff1b; 二是嵌入式Linux應用開發&#xf…

tinycore php,tinycore中文支持

這兩天在弄tinycore&#xff0c;想用它來搭個小系統出來。下載最新的CorePlus-current.iso 4.5.2&#xff0c;用開源的usb的安裝工具core2usb-1.6.exe將它安裝到u盤&#xff0c;然后接上臺式機上&#xff0c;開機從u盤啟動&#xff0c;一切順利。好&#xff0c;開始搞起&#…

javascript權威指南--學習筆記

-一、JavaScript基本數據類型 1、數字--Number類 2、字符串--String類 3、布爾--Boolean類 4、函數Function 5、對象Object 6、數組Array 7、null 8、undefined 備注&#xff1a; 當一個未定義的值用于布爾環境&#xff0c;他就會轉為false&#xff0c;用于數字環境就會轉為…

支持 dotnet 6 的 dnSpy 神器版本

官方的 dnSpy 在 2021 時&#xff0c;由于某些吃瓜的原因 wtfsck 將 dnSpy 給 Archived 掉&#xff0c;在大佬被哄好之前&#xff0c;預計是不再更新。最新官方版本對 dotnet 6 的支持較弱&#xff0c;對于很多 dotnet 6 應用都無法成功調試&#xff0c;附加調試上去將會讓應用…

cmd命令

mstsc 開啟遠程連接services.msc 開啟服務sc delete 服務名稱 刪除服務telnet 106.75.97.193 查看端口是否占用netstat -a 查看端口監聽狀態