国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Java Synchronized鎖失敗案例及解決方案

瀏覽:2日期:2022-09-02 10:48:57

synchronized關(guān)鍵字,一般稱之為”同步鎖“,用它來修飾需要同步的方法和需要同步代碼塊,默認(rèn)是當(dāng)前對象作為鎖的對象。

同步鎖鎖的是同一個對象,如果對象發(fā)生改變,則鎖會不生效。

鎖失敗的代碼:

public class IntegerSynTest { //線程實現(xiàn)Runnable接口 private static class Worker implements Runnable{ private Integer num; public Worker(Integer num){ this.num=num; } @Override public void run() { synchronized (num){Thread thread = Thread.currentThread();//System.identityHashCode:返回原生的hashCode值,不管Object對象是被重寫;空引用的哈希代碼為零System.out.println(thread.getName()+'--@:---'+System.identityHashCode(num));num++;System.out.println(thread.getName()+'------num:'+num+'---'+System.identityHashCode(num));try { Thread.sleep(1000);} catch (InterruptedException e) { e.printStackTrace();}System.out.println(thread.getName()+'------num:'+num+'---'+System.identityHashCode(num)); } } public static void main(String[] args) { Worker worker = new Worker(1); for (int i = 0; i < 5; i++) {new Thread(worker).start(); } } }}

鎖失敗的運行結(jié)果:

Java Synchronized鎖失敗案例及解決方案

鎖失敗的原因:

1.num++的.class實現(xiàn)是這樣的Integer integer1 = this.num, integer2 = this.num = Integer.valueOf(this.num.intValue() + 1);

2.查看 Integer.valueOf()的源代碼

Java Synchronized鎖失敗案例及解決方案

這時發(fā)現(xiàn),它是重新 new出一個新的Integer,這樣的話,每 ++一次,那么就會產(chǎn)生一個新的對象,而Synchronize鎖是鎖同一個對象,當(dāng)鎖不同對象時,則會鎖失敗。

解決方法:

Synchronized同步鎖只要鎖的對象不發(fā)生改變即可,那么由此只需要聲明一個對象,不修改它,鎖這一個對象即可(還有其他方法暫不一一列舉,以后也不會列舉了)。

鎖成功的代碼

public class IntegerSynTest { //線程實現(xiàn)Runnable接口 private static class Worker implements Runnable{ private Integer num; /** * ---重點看這里--- * 聲明要鎖的對象 * ---重點看這里--- */ private Object object = new Object(); public Worker(Integer num){ this.num=num; } @Override public void run() { //修改鎖對象 synchronized (num){Thread thread = Thread.currentThread();//System.identityHashCode:返回原生的hashCode值,不管Object對象是被重寫;空引用的哈希代碼為零System.out.println(thread.getName()+'--@:---'+System.identityHashCode(num));num++;System.out.println(thread.getName()+'------num:'+num+'---'+System.identityHashCode(num));try { Thread.sleep(1000);} catch (InterruptedException e) { e.printStackTrace();}System.out.println(thread.getName()+'------num:'+num+'---'+System.identityHashCode(num)); } } public static void main(String[] args) { Worker worker = new Worker(1); for (int i = 0; i < 5; i++) {new Thread(worker).start(); } } }}

鎖成功的運行結(jié)果:

Java Synchronized鎖失敗案例及解決方案

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 吉林市| 马龙县| 建瓯市| 静乐县| 新竹市| 孟村| 新闻| 平安县| 镇沅| 兴山县| 孝义市| 宣恩县| 晋城| 松原市| 阳朔县| 宜州市| 深州市| 肥东县| 青阳县| 淳化县| 凌云县| 云浮市| 青田县| 建宁县| 阜平县| 轮台县| 岑巩县| 宜兰市| 郯城县| 台湾省| 云阳县| 依兰县| 堆龙德庆县| 山东| 嘉荫县| 上栗县| 台山市| 沐川县| 乌兰县| 黄陵县| 双桥区|