java多线程(同步和死锁,生产者和消费者问题)
生活随笔
收集整理的這篇文章主要介紹了
java多线程(同步和死锁,生产者和消费者问题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先我們來看看同步與死鎖:
所謂死鎖。這是A有banana,B有apple。
A至B說:你把apple對我來說,,我會banana給你。
B至A說:你把banana對我來說,,我會apple給你。
可是A和B都在等待對方的答復。那么這樣終于的結果就是A得不到apple,B也得不到banana。這樣的死循環就是死鎖。
于是我們能夠模擬上面的描寫敘述,寫出下面代碼:
類A代表A這個人。
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這個人, 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.
從以上的程序執行,我們能夠發現,兩個線程都在等待對方的執行完畢。這樣,程序也就無法繼續執行。從而造成了死鎖執行現象。
以下我們來看生產者與消費者問題:
所謂生產者與消費者問題,非常easy,過程就是生產者不斷生產產品。消費者不斷取走產品。
Producer生產product。Consumer消費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();} }這里添加了等待與喚醒,并添加一個標志位flag,flag為true時,表示能夠生產。但不能取走,此時假設線程執行到了消費者線程,則應該等待,假設flag為false,則表示能夠取走,可是不能生產,假設生產者線程執行,則應該等待。
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();} }
所以我們知道。生產者每生產一個產品,刪除產品的消費者,刪除每個消費者產品。我們必須等待生產者生產。
總結
以上是生活随笔為你收集整理的java多线程(同步和死锁,生产者和消费者问题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 17家中国域名解析商(国际域名)解析量报
- 下一篇: [uva]AncientMessages