Java編程之多線(xiàn)程死鎖與線(xiàn)程間通信簡(jiǎn)單實(shí)現(xiàn)代碼
死鎖定義
死鎖是指兩個(gè)或者多個(gè)線(xiàn)程被永久阻塞的一種局面,產(chǎn)生的前提是要有兩個(gè)或兩個(gè)以上的線(xiàn)程,并且來(lái)操作兩個(gè)或者多個(gè)以上的共同資源;我的理解是用兩個(gè)線(xiàn)程來(lái)舉例,現(xiàn)有線(xiàn)程A和B同時(shí)操作兩個(gè)共同資源a和b,A操作a的時(shí)候上鎖LockA,繼續(xù)執(zhí)行的時(shí)候,A還需要LockB進(jìn)行下面的操作,這個(gè)時(shí)候b資源在被B線(xiàn)程操作,剛好被上了鎖LockB,假如此時(shí)線(xiàn)程B剛好釋放了LockB則沒(méi)有問(wèn)題,但沒(méi)有釋放LockB鎖的時(shí)候,線(xiàn)程A和B形成了對(duì)LockB鎖資源的爭(zhēng)奪,從而造成阻塞,形成死鎖;具體其死鎖代碼如下:
public class MyDeadLockTest { public static void main(String[] args){ Object obj1 = new Object(); Thread thread1 = new Thread(new DeadRes(true,obj1)); Thread thread2 = new Thread(new DeadRes(false,obj1)); thread1.start(); thread2.start(); } } class DeadRes implements Runnable{ boolean flag; Object obj; public DeadRes(boolean flag, Object obj1) { this.flag = flag; this.obj = obj1; } @Override public void run() { if(flag){ synchronized (DeadRes.class){ System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class"); synchronized (obj){ System.out.println(Thread.currentThread().getName()+" acquie lock is obj"); } } }else{ synchronized (obj){ System.out.println(Thread.currentThread().getName()+" acquie lock is obj"); synchronized (DeadRes.class){ System.out.println(Thread.currentThread().getName()+" acquie lock is DeadRes.class"); } } } } }
執(zhí)行結(jié)果如下圖:
Thread-1 acquie lock is obj Thread-0 acquie lock is DeadRes.class
當(dāng)然每次執(zhí)行的結(jié)果不一樣,有可能是一種和諧狀態(tài),沒(méi)有發(fā)生死鎖,此時(shí)為保證每次死鎖,可以讓run()方法中,執(zhí)行while(true)循環(huán),這樣保證了每次必定發(fā)生死鎖;當(dāng)然實(shí)際應(yīng)用中,我們應(yīng)該盡量避免死鎖,當(dāng)有多線(xiàn)程操作多個(gè)共同資源的時(shí)候,避免發(fā)生同一鎖對(duì)象的同步嵌套。
線(xiàn)程間的通訊—-生產(chǎn)者與消費(fèi)者模式
1、讓兩個(gè)線(xiàn)程交替進(jìn)行操作,當(dāng)生產(chǎn)了一個(gè)數(shù)字后,緊接著消費(fèi)一個(gè),首先采用Object對(duì)象中的wait-notify來(lái)實(shí)現(xiàn),具體代碼如下:
public class ThreadProConsume { public static void main(String[] args){ Product product = new Product(); Thread thread1 = new Thread(new Producer(product)); Thread thread2 = new Thread(new Consumer(product)); thread1.start(); thread2.start(); } } class Product{ String name; private int count = 1; boolean flag = false; public synchronized void set(String name){ if(flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name +"--"+count++; flag = true; System.out.println(Thread.currentThread().getName()+" produce num : "+this.name); this.notify(); } public synchronized void out(){ if(!flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name); flag = false; this.notify(); } } class Producer implements Runnable{ Product res; public Producer(Product product) { this.res = product; } @Override public void run() { while(true){ res.set("guyue"); } } } class Consumer implements Runnable{ Product res; public Consumer(Product product) { this.res = product; } @Override public void run() { while(true){ res.out(); } } }
執(zhí)行結(jié)果如圖:
Thread-1 consume num is : guyue--3938 Thread-0 produce num : guyue--3939 Thread-1 consume num is : guyue--3939 Thread-0 produce num : guyue--3940 Thread-1 consume num is : guyue--3940 Thread-0 produce num : guyue--3941 Thread-1 consume num is : guyue--3941
當(dāng)超過(guò)兩個(gè)以上線(xiàn)程操作的時(shí)候,這里需要在set()與out()方法中的if判斷改為while,并且notif方法,改為notifyAll(),這樣多個(gè)線(xiàn)程操作的時(shí)候,便可以交替進(jìn)行,具體代碼如下:
public class ThreadProConsume { public static void main(String[] args){ Product product = new Product(); Thread thread1 = new Thread(new Producer(product)); Thread thread3 = new Thread(new Producer(product)); Thread thread2 = new Thread(new Consumer(product)); Thread thread4 = new Thread(new Consumer(product)); thread1.start(); thread3.start(); thread2.start(); thread4.start(); } } class Product{ String name; private int count = 1; boolean flag = false; public synchronized void set(String name){ while(flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name +"--"+count++; flag = true; System.out.println(Thread.currentThread().getName()+" produce num : "+this.name); this.notifyAll(); } public synchronized void out(){ while (!flag){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+" consume num is : "+this.name); flag = false; this.notifyAll(); } }
執(zhí)行結(jié)果如下:
Thread-0 produce num : guyue--50325 Thread-2 consume num is : guyue--50325 Thread-1 produce num : guyue--50326 Thread-3 consume num is : guyue--50326 Thread-0 produce num : guyue--50327 Thread-2 consume num is : guyue--50327 Thread-1 produce num : guyue--50328 Thread-3 consume num is : guyue--50328
2、采用Lock-Condition方法實(shí)現(xiàn)如下:
class Product{ String name; private int count = 1; boolean flag = false; Lock lock = new ReentrantLock(); Condition conditon = lock.newCondition(); public void set(String name){ try{ lock.lock(); while(flag){ conditon.await(); } this.name = name +"--"+count++; flag = true; System.out.println(Thread.currentThread().getName()+" produce num : "+this.name); conditon.signalAll(); }catch (Exception e){ }finally { lock.unlock(); } } public void out(){ try{ lock.lock(); while(!flag){ conditon.await(); } flag = false; System.out.println(Thread.currentThread().getName()+" consumer num is : "+this.name); conditon.signalAll(); }catch (Exception e){ }finally { lock.unlock(); } } }
執(zhí)行結(jié)果如下:
Thread-0 produce num : guyue--20305 Thread-3 consumer num is : guyue--20305 Thread-1 produce num : guyue--20306 Thread-2 consumer num is : guyue--20306 Thread-0 produce num : guyue--20307 Thread-3 consumer num is : guyue--20307 Thread-1 produce num : guyue--20308 Thread-2 consumer num is : guyue--20308
以上就是本文關(guān)于Java編程之多線(xiàn)程死鎖與線(xiàn)程間通信簡(jiǎn)單實(shí)現(xiàn)代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。關(guān)于Java多線(xiàn)程以及線(xiàn)程間通信的例子,本站還有幾篇文章可以參考:
詳解java中的互斥鎖信號(hào)量和多線(xiàn)程等待機(jī)制、Java多線(xiàn)程編程小實(shí)例模擬停車(chē)場(chǎng)系統(tǒng)、Java網(wǎng)絡(luò)編程基礎(chǔ)篇之單向通信
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Java 線(xiàn)程池ExecutorService詳解及實(shí)例代碼
這篇文章主要介紹了Java 線(xiàn)程池ExecutorService詳解及實(shí)例代碼的相關(guān)資料,線(xiàn)程池減少在創(chuàng)建和銷(xiāo)毀線(xiàn)程上所花的時(shí)間以及系統(tǒng)資源的開(kāi)銷(xiāo).如果不使用線(xiàn)程池,有可能造成系統(tǒng)創(chuàng)建大量線(xiàn)程而導(dǎo)致消耗系統(tǒng)內(nèi)存以及”過(guò)度切換“2016-11-11Mybatis Integer類(lèi)型參數(shù)值為0時(shí)得到為空的解決方法
這篇文章主要介紹了Mybatis Integer類(lèi)型參數(shù)值為0時(shí)得到為空的解決方法,有需要的朋友們可以學(xué)習(xí)下。2019-08-08SpringCloud Edgware.SR3版本中Ribbon的timeout設(shè)置方法
今天小編就為大家分享一篇關(guān)于SpringCloud Edgware.SR3版本中Ribbon的timeout設(shè)置方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12淺談HTTP使用BASIC認(rèn)證的原理及實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇淺談HTTP使用BASIC認(rèn)證的原理及實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11SpringBoot中動(dòng)態(tài)數(shù)據(jù)源是實(shí)現(xiàn)與用途
這篇文章主要是來(lái)和大家討論一下SpringBoot中動(dòng)態(tài)數(shù)據(jù)源是實(shí)現(xiàn)與用途,文中的示例代碼簡(jiǎn)潔易懂,具有一定的學(xué)習(xí)價(jià)值,感興趣的可以了解一下2023-08-08kafka并發(fā)寫(xiě)大消息異常TimeoutException排查記錄
這篇文章主要為大家介紹了kafka并發(fā)寫(xiě)大消息異常TimeoutException的排查記錄及解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02