日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java多线程(同步和死锁,生产者和消费者问题)

發(fā)布時(shí)間:2025/1/21 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java多线程(同步和死锁,生产者和消费者问题) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

首先我們來(lái)看看同步與死鎖:


所謂死鎖。這是A有banana,B有apple。

A至B說(shuō):你把a(bǔ)pple對(duì)我來(lái)說(shuō),,我會(huì)banana給你。

B至A說(shuō):你把banana對(duì)我來(lái)說(shuō),,我會(huì)apple給你。


可是A和B都在等待對(duì)方的答復(fù)。那么這樣終于的結(jié)果就是A得不到apple,B也得不到banana。這樣的死循環(huán)就是死鎖。

于是我們能夠模擬上面的描寫敘述,寫出下面代碼:


類A代表A這個(gè)人。

public class A {public void say(){System.out.println("A said to B: if you give me the apple, I will give you the banana.");}public void get(){System.out.println("A get the apple.");} }
類B代表B這個(gè)人,

public class B {public void say(){System.out.println("B said to A: if you give me the banana, I will give you the apple.");}public void get(){System.out.println("B get the banana.");} }
類ThreadDeadLock代表死鎖類,

public class ThreadDeadLock implements Runnable{private static A a=new A();private static B b=new B();public boolean flag=false;public void run(){if(flag){synchronized(a){a.say();try{Thread.sleep(500);}catch(InterruptedException e){e.printStackTrace();}synchronized(b){a.get();}}}else{synchronized(b){b.say();try{Thread.sleep(500);}catch(InterruptedException e){e.printStackTrace();}synchronized(a){b.get();}}}} }
以下是主類:

public class Main{public static void main(String[] args){ThreadDeadLock t1=new ThreadDeadLock();ThreadDeadLock t2=new ThreadDeadLock();t1.flag=true;t2.flag=false;Thread thA=new Thread(t1);Thread thB=new Thread(t2);thA.start();thB.start();} }


程序執(zhí)行結(jié)果:

A said to B: if you give me the apple, I will give you the banana.
B said to A: if you give me the banana, I will give you the apple.


從以上的程序執(zhí)行,我們能夠發(fā)現(xiàn),兩個(gè)線程都在等待對(duì)方的執(zhí)行完畢。這樣,程序也就無(wú)法繼續(xù)執(zhí)行。從而造成了死鎖執(zhí)行現(xiàn)象。


以下我們來(lái)看生產(chǎn)者與消費(fèi)者問(wèn)題:


所謂生產(chǎn)者與消費(fèi)者問(wèn)題,非常easy,過(guò)程就是生產(chǎn)者不斷生產(chǎn)產(chǎn)品。消費(fèi)者不斷取走產(chǎn)品。

Producer生產(chǎn)product。Consumer消費(fèi)product。

于是,我們先定義product類:

public class Product {private String name="product";private boolean flag=false;public String getName() {return name;}public void setName(String name) {this.name = name;}public synchronized void set(String name){if(!flag){try{super.wait();}catch(InterruptedException e){e.printStackTrace();}}this.setName(name);try{Thread.sleep(300);}catch(InterruptedException e){e.printStackTrace();}flag=false;super.notify();}public synchronized void get(){if(flag){try{super.wait();}catch(InterruptedException e){e.printStackTrace();}}try{Thread.sleep(300);}catch(InterruptedException e){e.printStackTrace();}System.out.println(this.getName());flag=true;super.notify();} }
這里添加了等待與喚醒,并添加一個(gè)標(biāo)志位flag,flag為true時(shí),表示能夠生產(chǎn)。但不能取走,此時(shí)假設(shè)線程執(zhí)行到了消費(fèi)者線程,則應(yīng)該等待,假設(shè)flag為false,則表示能夠取走,可是不能生產(chǎn),假設(shè)生產(chǎn)者線程執(zhí)行,則應(yīng)該等待。


Producer類:

public class Producer implements Runnable{private Product product=null;public Producer(Product product){this.product=product;}public void run(){for(int i=0;i<50;++i){this.product.set("product");}} }
Consumer類:

public class Consumer implements Runnable{private Product product=null;public Consumer(Product product){this.product=product;}public void run(){for(int i=0;i<50;++i){try{Thread.sleep(100);}catch(InterruptedException e){e.printStackTrace();}this.product.get();}} }
然后是主類:

public class Main{public static void main(String[] args){Product product=new Product();Producer pro=new Producer(product);Consumer con=new Consumer(product);new Thread(pro).start();new Thread(con).start();} }
所以我們知道。生產(chǎn)者每生產(chǎn)一個(gè)產(chǎn)品,刪除產(chǎn)品的消費(fèi)者,刪除每個(gè)消費(fèi)者產(chǎn)品。我們必須等待生產(chǎn)者生產(chǎn)。

總結(jié)

以上是生活随笔為你收集整理的java多线程(同步和死锁,生产者和消费者问题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。