當(dāng)前位置:
首頁(yè) >
synchronized 的理解
發(fā)布時(shí)間:2023/12/18
57
豆豆
生活随笔
收集整理的這篇文章主要介紹了
synchronized 的理解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
第一種:package?com.ucmed.zsyy.util; /** *?Created?by?ucmed?on?2017/2/8. */ public?class?DirtyRead?{ private?String?username?=?"zjkj"; private?String?password?=?"123"; public?synchronized?void?setValue(String?username,?String?password)?{ System.out.println("set線程:"?+?Thread.currentThread().getPriority()); this.username?=?username; try?{ Thread.sleep(2000); }?catch(InterruptedException?e)?{ e.printStackTrace(); } this.password?=?password; System.out.println("setValue最終結(jié)果:username?=?"?+?username?+?"??,?password?=?"?+?password); } public?void?getValue()?{ System.out.println("get線程:"?+?Thread.currentThread().getPriority()); System.out.println("getValue最終結(jié)果:username?=?"?+?username?+?"??,?password?=?"?+?password); } public?static?void?main(String[]?args)?throws?InterruptedException?{ final?DirtyRead?dr?=?new?DirtyRead(); Thread?t1?=?new?Thread(new?Runnable()?{ @Override public?void?run()?{ dr.setValue("z3",?"456"); } }); System.out.println("主線程:"?+?Thread.currentThread().getPriority()); t1.start(); Thread.sleep(1000); dr.getValue(); } } 第二種: package?com.ucmed.zsyy.util; /** *?Created?by?ucmed?on?2017/2/8. */ public?class?DirtyRead?{ private?String?username?=?"zjkj"; private?String?password?=?"123"; public?synchronized?void?setValue(String?username,?String?password)?{ System.out.println("set線程:"?+?Thread.currentThread().getPriority()); this.username?=?username; try?{ Thread.sleep(2000); }?catch(InterruptedException?e)?{ e.printStackTrace(); } this.password?=?password; System.out.println("setValue最終結(jié)果:username?=?"?+?username?+?"??,?password?=?"?+?password); } public?synchronized?void?getValue()?{ System.out.println("get線程:"?+?Thread.currentThread().getPriority()); System.out.println("getValue最終結(jié)果:username?=?"?+?username?+?"??,?password?=?"?+?password); } public?static?void?main(String[]?args)?throws?InterruptedException?{ final?DirtyRead?dr?=?new?DirtyRead(); Thread?t1?=?new?Thread(new?Runnable()?{ @Override public?void?run()?{ dr.setValue("z3",?"456"); } }); System.out.println("主線程:"?+?Thread.currentThread().getPriority()); t1.start(); Thread.sleep(1000); dr.getValue(); } } 理解: 這里只有一個(gè)對(duì)象,一個(gè)對(duì)象的多個(gè)synchronized修飾的方法的情況
上面例子是當(dāng)get方法加上鎖后,要等待set方法執(zhí)行完后再執(zhí)行 在方法上加synchronized關(guān)鍵字,持有的鎖為當(dāng)前對(duì)象,當(dāng)一個(gè)線程調(diào)用了其中一個(gè)synchronized的方法后,其他線程再調(diào)用該類中其他synchronized修飾的方法會(huì)掛起等待,需要等上一個(gè)線程執(zhí)行完,釋放鎖后,其他調(diào)用線程取得鎖后才會(huì)執(zhí)行 不是多個(gè)鎖,是多個(gè)synchronized修飾的方法..這些方法是同一個(gè)鎖
上面例子是當(dāng)get方法加上鎖后,要等待set方法執(zhí)行完后再執(zhí)行 在方法上加synchronized關(guān)鍵字,持有的鎖為當(dāng)前對(duì)象,當(dāng)一個(gè)線程調(diào)用了其中一個(gè)synchronized的方法后,其他線程再調(diào)用該類中其他synchronized修飾的方法會(huì)掛起等待,需要等上一個(gè)線程執(zhí)行完,釋放鎖后,其他調(diào)用線程取得鎖后才會(huì)執(zhí)行 不是多個(gè)鎖,是多個(gè)synchronized修飾的方法..這些方法是同一個(gè)鎖
轉(zhuǎn)載于:https://www.cnblogs.com/heqiang/p/6386122.html
總結(jié)
以上是生活随笔為你收集整理的synchronized 的理解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IQueryable和IEnumerab
- 下一篇: 简单交互