java多线程(同步和死锁,生产者和消费者问题)
首先我們來(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();} }
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)題。
- 上一篇: 17家中国域名解析商(国际域名)解析量报
- 下一篇: [uva]AncientMessages