Java多線程+鎖機制實現(xiàn)簡單模擬搶票的項目實踐
前言
鎖是一種同步機制,用于控制對共享資源的訪問。鎖的作用是確保同一時間只有一個線程可以訪問共享資源,也就是說保證了線程安全。因此在并發(fā)編程中,鎖是相當重要的。
一、基本概念
1.為什么需要鎖?
(1)多任務環(huán)境中才需要;
(2)任務都需要對同一共享資源進行寫操作;
(3)對資源的訪問是互斥的;
2.Syncronized與Lock的區(qū)別
(1)syncronized是jvm層面的內(nèi)置關鍵字,lock是java的一個接口;
(2)syncronized實現(xiàn)線程同步時,若線程一阻塞,線程二則一直等待,lock則不會,會自動結(jié)束線程;
(3)syncronized會自動釋放鎖,lock需要手動在finally里釋放(unlock),syncronized無法判斷是否獲得鎖的狀態(tài),lock可以;
(4)syncronized的鎖可重入、不可中斷、非公平,lock的鎖可重入、可中斷、公平;
(5)lock適合大量同步代碼的同步問題,syncronized適合少量;
3.常見的鎖
(1)sychronized:非公平、悲觀、獨享、互斥、可重入的重量級鎖
(2)ReentrantLock:默認非公平但可實現(xiàn)公平的、悲觀、獨享、互斥、可重入、重量級鎖
(3)ReentrantReadWriteLock:默認非公平但是可實現(xiàn)公平的、悲觀、寫獨享、讀共享、讀寫、可重入、重量級鎖
4.缺點
jvm鎖解決不了分布式環(huán)境多任務對共享資源競爭的協(xié)同操作問題。
二、模擬搶票
(1)不用鎖的情況,會出現(xiàn)重賣問題,應避免重賣還有超賣問題
public class Main { public static void main(String[] args) { DiyThread diyThread = new DiyThread(); Thread t1 = new Thread(diyThread,"窗口A"); Thread t2 = new Thread(diyThread,"窗口B"); Thread t3 = new Thread(diyThread,"窗口C"); Thread t4 = new Thread(diyThread,"窗口D"); t1.start(); t2.start(); t3.start(); t4.start(); } } class DiyThread implements Runnable { // 成員變量(實例變量):隨著對象的創(chuàng)建而存在,隨著對象的回收而釋放,存儲在堆內(nèi)存的對象中 private int count = 100; // 靜態(tài)變量(類變量):隨著類的加載而存在,隨著類的消失而消失,存儲在方法區(qū)(共享數(shù)據(jù)區(qū))的靜態(tài)區(qū) // private static int count = 100; public void run() { while (true) { if (count > 0) { // 不用鎖的情況,會出現(xiàn)重賣問題 System.out.println(Thread.currentThread().getName() + " -> 售出第" + count + "張火車票"); count--; try { // 睡眠50毫秒 Thread.sleep(50); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " : 出現(xiàn)異常 -> " + e.getMessage()); } } else { System.out.println(Thread.currentThread().getName() + " : 票已售完"); break; } } } }
(2)使用【同步代碼塊】使線程串行同步,不然會出現(xiàn)線程不安全的問題
public class Main { public static void main(String[] args) { DiyThread diyThread = new DiyThread(); Thread t1 = new Thread(diyThread,"窗口A"); Thread t2 = new Thread(diyThread,"窗口B"); Thread t3 = new Thread(diyThread,"窗口C"); Thread t4 = new Thread(diyThread,"窗口D"); t1.start(); t2.start(); t3.start(); t4.start(); } } class DiyThread implements Runnable { // 成員變量(實例變量):隨著對象的創(chuàng)建而存在,隨著對象的回收而釋放,存儲在堆內(nèi)存的對象中 private int count = 100; // 靜態(tài)變量(類變量):隨著類的加載而存在,隨著類的消失而消失,存儲在方法區(qū)(共享數(shù)據(jù)區(qū))的靜態(tài)區(qū) // private static int count = 100; public void run() { while (true) { synchronized(this) { if (count > 0) { System.out.println(Thread.currentThread().getName() + " -> 售出第" + count + "張火車票"); count--; try { // 睡眠50毫秒 Thread.sleep(50); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " : 出現(xiàn)異常 -> " + e.getMessage()); } } else { System.out.println(Thread.currentThread().getName() + " : 票已售完"); break; } } } } }
(3)使用【synchronized】同步函數(shù)使線程串行同步,不然會出現(xiàn)線程不安全的問題
public class Main { public static void main(String[] args) { DiyThread diyThread = new DiyThread(); Thread t1 = new Thread(diyThread,"窗口A"); Thread t2 = new Thread(diyThread,"窗口B"); Thread t3 = new Thread(diyThread,"窗口C"); Thread t4 = new Thread(diyThread,"窗口D"); t1.start(); t2.start(); t3.start(); t4.start(); } } class DiyThread implements Runnable { // 成員變量(實例變量):隨著對象的創(chuàng)建而存在,隨著對象的回收而釋放,存儲在堆內(nèi)存的對象中 private int count = 100; // 靜態(tài)變量(類變量):隨著類的加載而存在,隨著類的消失而消失,存儲在方法區(qū)(共享數(shù)據(jù)區(qū))的靜態(tài)區(qū) // private static int count = 100; public void run() { sale(); } private synchronized void sale() { while (true) { if (count > 0) { System.out.println(Thread.currentThread().getName() + " -> 售出第" + count + "張火車票"); count--; try { // 睡眠50毫秒 Thread.sleep(50); } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " : 出現(xiàn)異常 -> " + e.getMessage()); } } else { System.out.println(Thread.currentThread().getName() + " : 票已售完"); break; } } } }
(4)使用【java.util.concurrent】簡稱juc包的lock鎖
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Main { public static void main(String[] args) { DiyThread diyThread = new DiyThread(); Thread t1 = new Thread(diyThread,"窗口A"); Thread t2 = new Thread(diyThread,"窗口B"); Thread t3 = new Thread(diyThread,"窗口C"); Thread t4 = new Thread(diyThread,"窗口D"); t1.start(); t2.start(); t3.start(); t4.start(); } } class DiyThread implements Runnable { // 成員變量(實例變量):隨著對象的創(chuàng)建而存在,隨著對象的回收而釋放,存儲在堆內(nèi)存的對象中 private int count = 100; // 靜態(tài)變量(類變量):隨著類的加載而存在,隨著類的消失而消失,存儲在方法區(qū)(共享數(shù)據(jù)區(qū))的靜態(tài)區(qū) // private static int count = 100; private Lock lock = new ReentrantLock(); public void run() { while (true) { lock.lock(); // 加鎖之后,以下的業(yè)務代碼就是單線程環(huán)境運行,如4個線程競爭這把鎖 try { if (count > 0) { System.out.println(Thread.currentThread().getName() + " -> 售出第" + count + "張火車票"); count--; Thread.sleep(50); } else { System.out.println(Thread.currentThread().getName() + " : 票已售完"); break; } } catch (Exception e) { System.out.println(Thread.currentThread().getName() + " : 出現(xiàn)異常 -> " + e.getMessage()); } finally { lock.unlock(); // 解鎖,最重要原因為避免死鎖,無論正確、異常執(zhí)行,都執(zhí)行解鎖 } } } }
到此這篇關于Java多線程+鎖機制實現(xiàn)簡單模擬搶票的項目實踐的文章就介紹到這了,更多相關Java 模擬搶票內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot封裝自己的Starter的實現(xiàn)方法
這篇文章主要介紹了SpringBoot封裝自己的Starter的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04springboot整合websocket后啟動報錯(javax.websocket.server.ServerCont
這篇文章主要介紹了springboot整合websocket后啟動報錯(javax.websocket.server.ServerContainer not available),通過分析錯誤信息、排查代碼和配置,找出問題的根源,并給出相應的解決方案,感興趣的可以了解一下2024-01-01Mybatis自定義攔截器實現(xiàn)權(quán)限功能
本文主要介紹了Mybatis自定義攔截器實現(xiàn)權(quán)限功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-12-12