Java多線程的臨界資源問題解決方案
這篇文章主要介紹了Java多線程的臨界資源問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
臨界資源問題的原因:某一個線程在對臨界資源進行訪問時,還沒來得及完全修改臨界資源的值,臨界資源就被其他線程拿去訪問,導致多個線程訪問同一資源。直觀表現(xiàn)為打印結果順序混亂。
解決方法:加鎖
靜態(tài)方法中用類鎖,非靜態(tài)方法中用對象鎖。
1.同步代碼段:synchronized(){...}
2.同步方法:使用關鍵字synchronized修飾的方法
3.使用顯式同步鎖ReentrantLock
鎖池描述的即為鎖外等待的狀態(tài)
方法一:同步代碼段:synchronized(){...}
public class SourceConflict {
public static void main(String[] args) {
//實例化4個售票員,用4個線程模擬4個售票員
Runnable r = () -> {
while (TicketCenter.restCount > 0) {
synchronized(" ") {
if (TicketCenter.restCount <= 0) {
return;
}
System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --TicketCenter.restCount + "張票");
}
}
};
//用4個線程模擬4個售票員
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");
//開啟線程
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
//實現(xiàn)四名售票員共同售票,資源共享,非獨立
//Lambda表達式或匿名內(nèi)部類內(nèi)部捕獲的局部變量必須顯式的聲明為 final 或?qū)嶋H效果的的 final 類型,而捕獲實例或靜態(tài)變量是沒有限制的
class TicketCenter{
public static int restCount = 100;
}
方法二:同步方法,即使用關鍵字synchronized修飾的方法
public class SourceConflict2 {
public static void main(String[] args) {
//實例化4個售票員,用4個線程模擬4個售票員
Runnable r = () -> {
while (TicketCenter.restCount > 0) {
sellTicket();
}
};
//用4個線程模擬4個售票員
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");
//開啟線程
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
private synchronized static void sellTicket() {
if (TicketCenter.restCount <= 0) {
return;
}
System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --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) {
//實例化4個售票員,用4個線程模擬4個售票員
//顯式鎖
ReentrantLock lock = new ReentrantLock();
Runnable r = () -> {
while (TicketCenter.restCount > 0) {
lock.lock();
if (TicketCenter.restCount <= 0) {
return;
}
System.out.println(Thread.currentThread().getName() + "賣出一張票,剩余" + --TicketCenter.restCount + "張票");
lock.unlock();
}
};
//用4個線程模擬4個售票員
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");
//開啟線程
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class TicketCenter{
public static int restCount = 100;
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringAop @Around執(zhí)行兩次的原因及解決
這篇文章主要介紹了SpringAop @Around執(zhí)行兩次的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
java8中的List<String>轉(zhuǎn)List<Integer>的實例代碼
這篇文章主要介紹了java8中的List<String>轉(zhuǎn)List<Integer>,轉(zhuǎn)換list列表String到列表Intger,java8提供了stream很好的進行操作,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-07-07
mybatis參數(shù)類型不匹配錯誤argument type mismatch的處理方案
這篇文章主要介紹了mybatis參數(shù)類型不匹配錯誤argument type mismatch的處理方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Jenkins Pipeline 部署 SpringBoot 應用的教程詳解
這篇文章主要介紹了Jenkins Pipeline 部署 SpringBoot 應用的詳細教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

