付費下載
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、在Java語言中,引入對象互斥鎖的概念,保證共享數(shù)據(jù)操作的完整性。每個對象都對應(yīng)于一個可稱為互斥鎖的標(biāo)記,這個標(biāo)記保證在任一時刻,只能有一個線程訪問對象。關(guān)鍵字synchronized用來給對象加互斥鎖。三國1同步非靜態(tài)方法:synchronized放在方法聲明中,表示整個方法為同步方法,鎖定this對象,如果有一個線程進(jìn)入了該方法,其他線程要想使用當(dāng)前this對象的任何同步方法,都必須等待前一個線程執(zhí)行完該同步方法之后public synchronized void method1() public synchronized void method2() public synchronize
2、d void method3() 同步static方法: synchronized放在static方法聲明中,表示鎖定該類的class對象如果有一個線程進(jìn)入了該方法,其他線程要想使用當(dāng)前類中的任何同步靜態(tài)方法,都必須等待前一個線程執(zhí)行完該同步方法之后;其他非同步方法及非靜態(tài)的同步方法的執(zhí)行不受影響.3public synchronized static void method1() public synchronized static void method2() 同步代碼塊:synchronized放在對象前面限制一段代碼的執(zhí)行4Object obj = new Object();.sync
3、hronized(this)/this被加鎖,任何其他要鎖this的代碼塊被阻塞. 需要同步的代碼;.synchronized(obj)/obj被加鎖,任何其他要鎖obj的代碼塊被阻塞. 需要同步的代碼;.class TicketOffice implements Runnable private int tickets = 0; private boolean flag =true; public void run() while (flag) try Thread.sleep(10); catch (InterruptedException e) e.printStackTrace();
4、if (tickets 100) tickets+; System.out.println(Thread.currentThread().getName()+ :賣出第 + tickets + 張票); else flag = false; public class TestTicketOffice public static void main(String args) TicketOffice off = new TicketOffice(); Thread thread1 = new Thread(off); thread1.setName(售票點1); thread1.start();
5、 5售票案例中出現(xiàn)的數(shù)據(jù)不一致問題class TicketOffice implements Runnable private int tickets = 0; private boolean flag = true; public void run() while (flag) try Thread.sleep(10); catch (InterruptedException e) e.printStackTrace(); synchronized(this) if (tickets 100) tickets+; System.out.println(Thread.currentThread
6、().getName()+ :賣出第 + tickets + 張票); else flag = false; public synchronized void sale() if (tickets 100) tickets+; System.out.println(Thread.currentThread().getName() + :賣出第 + tickets + 張票); else flag = false; 6當(dāng)run()方法體內(nèi)的代碼操作到了成員變量(共享數(shù)據(jù))時,就可能會出現(xiàn)多線程安全問題(線程不同步問題)。編程技巧在方法中盡量少操作成員變量,多使用局部變量7public class
7、 TestSyncExercise public static void main(String args) MyRunn run = new MyRunn(); Thread thread1 = new Thread(run); thread1.start(); try Thread.sleep(1000); catch (InterruptedException e) e.printStackTrace(); run.test2(); class MyRunn implements Runnable private int count = 100; public void run() te
8、st1(); public synchronized void test1() System.out.println(test1開始執(zhí)行,把count改成1000); count = 1000; try Thread.sleep(5000); catch (InterruptedException e) e.printStackTrace(); System.out.println(test1:count= + count); public void test2() System.out.println(test2開始執(zhí)行,把count改成2000); count = 2000; System
9、.out.println(test2:count= + count); TestSyncExercise.java8同步練習(xí)設(shè)計一個銀行賬戶Account類(name,money, saveOrWithdrawMoney(int x),定義一個線程類MyThread 一個場景類AccountTest,創(chuàng)建一個Account對象, 創(chuàng)建多個線程類的對象,通過線程類的構(gòu)造方法傳入同一個Account的對象和增減的錢數(shù),并啟動,模擬銀行卡存取錢的過程;分別不使用/使用同步,觀察結(jié)果同步效果:現(xiàn)在有錢:1000準(zhǔn)備再存100存完,現(xiàn)在有錢:1100現(xiàn)在有錢:1100準(zhǔn)備再存400存完,現(xiàn)在有錢:150
10、0現(xiàn)在有錢:1500準(zhǔn)備再取-100取完,現(xiàn)在有錢:1400場景類代碼提示見下頁同步練習(xí)Account account = new Account(zhangsan,1000);MyThread t1 = new MyThread(account, 100);MyThread t2 = new MyThread(account, -100);MyThread t3 = new MyThread(account, 400);t1.start();t2.start();t3.start();在線程類MyThread的run()方法中調(diào)用saveOrWithdrawMoney集合容器類的線程安全性的
11、問題Runnable實現(xiàn)類:ArrayList al = new ArrayList();/List al2 = Collections.synchronizedList(al);/Vector al3 = new Vector();public void run() for(int i = 0;i 10000;i+)/synchronized(al)al.add(i);al.add(i);System.out.println(Thread.currentThread().getName() +已經(jīng)運行完畢);死鎖的原因:線程1鎖住資源A等待資源B,線程2鎖住資源B等待資源A,兩個線程都在等待
12、自己需要的資源,而這些資源被另外的線程鎖住,這些線程你等我,我等你,誰也不愿意讓出資源,這樣死鎖就產(chǎn)生了。12public class TestDeadLock implements Runnable public boolean flag = true; private static Object res1 = new Object(); private static Object res2 = new Object(); public void run() if (flag) /* 鎖定資源res1 */ synchronized (res1) System.out.println(鎖定資
13、源1,等待資源2.); try Thread.sleep(1000); catch (InterruptedException e) /* 鎖定資源res2 */ synchronized (res2) System.out.println(Complete.); else /* 鎖定資源res2 */ synchronized (res2) System.out.println(鎖定資源2,等待資源1*); try Thread.sleep(1000); catch (InterruptedException e) /* 鎖定資源res1 */ synchronized (res1) System.out.println(Complete.); public static void mai
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- CCAA - 2015年03月建筑施工領(lǐng)域?qū)I(yè)答案及解析 - 詳解版(70題)
- 河北省保定市六校聯(lián)考2025-2026學(xué)年高一上學(xué)期1月期末考試語文試題(含答案)
- 2025-2026學(xué)年學(xué)年度第一學(xué)期期末質(zhì)量監(jiān)測高二英語科試題(含答案)
- 養(yǎng)老院消防演練制度
- 養(yǎng)老院定期體檢制度
- 智能垃圾桶生產(chǎn)建設(shè)項目環(huán)評報告
- 老年終末期跌倒預(yù)防的多靶點干預(yù)策略
- 冷空氣影響期間交通安全管理規(guī)定
- 2025年阜陽潁上縣城鄉(xiāng)水務(wù)有限公司招聘考試真題
- 海鹽采收工安全宣傳水平考核試卷含答案
- 彩盒成品檢驗標(biāo)準(zhǔn)
- 酒店治安安全管理制度范本
- 塑膠-施工方案
- DB32∕T 4700-2024 蓄熱式焚燒爐系統(tǒng)安全技術(shù)要求
- 2025-2030光器件行業(yè)人才缺口現(xiàn)狀與高端人才培養(yǎng)體系建設(shè)報告
- 物業(yè)入戶維修標(biāo)準(zhǔn)及流程
- GB/T 19839-2025工業(yè)燃油燃?xì)馊紵魍ㄓ眉夹g(shù)條件
- 生物濾池除臭裝置設(shè)計計算實例
- 銀行資產(chǎn)池管理辦法
- 選煤廠安全規(guī)程培訓(xùn)考核試題帶答案
- 人音版七年級音樂上冊說課稿:2.4 藍(lán)色的探戈
評論
0/150
提交評論