您好,登錄后才能下訂單哦!
Java多線程的臨界資源問(wèn)題的解決方法,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
臨界資源問(wèn)題的原因:某一個(gè)線程在對(duì)臨界資源進(jìn)行訪問(wèn)時(shí),還沒(méi)來(lái)得及完全修改臨界資源的值,臨界資源就被其他線程拿去訪問(wèn),導(dǎo)致多個(gè)線程訪問(wèn)同一資源。直觀表現(xiàn)為打印結(jié)果順序混亂。
解決方法:加鎖
靜態(tài)方法中用類鎖,非靜態(tài)方法中用對(duì)象鎖。
1.同步代碼段:synchronized(){...}
2.同步方法:使用關(guān)鍵字synchronized修飾的方法
3.使用顯式同步鎖ReentrantLock
鎖池描述的即為鎖外等待的狀態(tài)
方法一:同步代碼段:synchronized(){...}
public class SourceConflict { public static void main(String[] args) { //實(shí)例化4個(gè)售票員,用4個(gè)線程模擬4個(gè)售票員 Runnable r = () -> { while (TicketCenter.restCount > 0) { synchronized(" ") { if (TicketCenter.restCount <= 0) { return; } System.out.println(Thread.currentThread().getName() + "賣(mài)出一張票,剩余" + --TicketCenter.restCount + "張票"); } } }; //用4個(gè)線程模擬4個(gè)售票員 Thread thread1 = new Thread(r, "thread-1"); Thread thread2 = new Thread(r, "thread-2"); Thread thread3 = new Thread(r, "thread-3"); Thread thread4 = new Thread(r, "thread-4"); //開(kāi)啟線程 thread1.start(); thread2.start(); thread3.start(); thread4.start(); } }//實(shí)現(xiàn)四名售票員共同售票,資源共享,非獨(dú)立//Lambda表達(dá)式或匿名內(nèi)部類內(nèi)部捕獲的局部變量必須顯式的聲明為 final 或?qū)嶋H效果的的 final 類型,而捕獲實(shí)例或靜態(tài)變量是沒(méi)有限制的class TicketCenter{ public static int restCount = 100; }
方法二:同步方法,即使用關(guān)鍵字synchronized修飾的方法
public class SourceConflict2 { public static void main(String[] args) { //實(shí)例化4個(gè)售票員,用4個(gè)線程模擬4個(gè)售票員 Runnable r = () -> { while (TicketCenter.restCount > 0) { sellTicket(); } }; //用4個(gè)線程模擬4個(gè)售票員 Thread thread1 = new Thread(r, "thread-1"); Thread thread2 = new Thread(r, "thread-2"); Thread thread3 = new Thread(r, "thread-3"); Thread thread4 = new Thread(r, "thread-4"); //開(kāi)啟線程 thread1.start(); thread2.start(); thread3.start(); thread4.start(); } private synchronized static void sellTicket() { if (TicketCenter.restCount <= 0) { return; } System.out.println(Thread.currentThread().getName() + "賣(mài)出一張票,剩余" + --TicketCenter.restCount + "張票"); }}class TicketCenter{ public static int restCount = 100; }
方法三:使用顯式同步鎖ReentrantLock
import java.util.concurrent.locks.ReentrantLock;public class SourceConflict3 { public static void main(String[] args) { //實(shí)例化4個(gè)售票員,用4個(gè)線程模擬4個(gè)售票員 //顯式鎖 ReentrantLock lock = new ReentrantLock(); Runnable r = () -> { while (TicketCenter.restCount > 0) { lock.lock(); if (TicketCenter.restCount <= 0) { return; } System.out.println(Thread.currentThread().getName() + "賣(mài)出一張票,剩余" + --TicketCenter.restCount + "張票"); lock.unlock(); } }; //用4個(gè)線程模擬4個(gè)售票員 Thread thread1 = new Thread(r, "thread-1"); Thread thread2 = new Thread(r, "thread-2"); Thread thread3 = new Thread(r, "thread-3"); Thread thread4 = new Thread(r, "thread-4"); //開(kāi)啟線程 thread1.start(); thread2.start(); thread3.start(); thread4.start(); } }class TicketCenter{ public static int restCount = 100; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。