Java 多線程之併發協做生產者消費者設計模式 JDK1.5.0 + 升級優化版

轉載請註明出處:blog.csdn.net/linglongxin…java


上一篇[Java多線程之併發協做生產者消費者設計模式]已經講述了在Java的多線程中,如何處理併發安全的生產者消費者設計模式,不瞭解的能夠先看看上一篇的內容設計模式

#上一篇中的效率和優化問題安全

  • 在上一篇中生產者消費者設計模式中最後爲了避免產生相似於「死鎖」,就是多線程所有wait()致使程序沒法向下繼續運行的狀況,咱們採用了notifyAll()這個方法,那麼有什麼問題呢?
  • 效率問題,notifyAll()方法是將在執行了wait()方法後的全部等待線程都喚醒了,那麼也就是說,即喚醒了生產者線程也喚醒了消費者線程
  • 優化方案:咱們沒有必要把你們都喚醒,只須要讓生產者線程喚醒消費者線程,消費者線程喚醒生產者線程便可。

#jdk1.5的對多線程併發安全的新解決方案多線程

  • jdk1.5之後將同步和鎖封裝成了對象。並將操做鎖的隱式方式定義到了該對象中,將隱式動做變成了顯示動做。
  • Lock接口: 出現替代了同步代碼塊或者同步函數。將同步的隱式鎖操做變成現實鎖操做。同時更爲靈活。能夠一個鎖上加上多組監視器。
  • lock():獲取鎖。
  • unlock():釋放鎖,一般須要定義finally代碼塊中。
  • Condition接口:出現替代了Object中的wait notify notifyAll方法。將這些監視器方法單獨進行了封裝,變成Condition監視器對象。能夠任意鎖進行組合。
  • await();線程等待—>等同於wait();【監視器的方法】
  • signal();喚醒線程——>等同於notify();【監視器的方法】
  • signalAll();喚醒所有等待中的線程——>等同於notifyAll();【監視器的方法】

#最終通過優化的代碼併發

  • Resource.java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/** * Created by yuandl on 2016-10-12. * 要生產的資源 */
public class Resource {
    private int number = 0;//資源編號
    private boolean flag = false;//資源標記 時候有資源

    /* 建立一個鎖對象。*/
    private Lock lock = new ReentrantLock();//鎖
    /*經過已有的鎖獲取該鎖上的監視器對象。經過已有的鎖獲取兩組監視器,一組監視生產者,一組監視消費者。*/
    private Condition condition_consumer = lock.newCondition();//消費者對鎖的監視器
    private Condition condition_producer = lock.newCondition();//生產者對鎖的監視器

    public void create() {
        lock.lock();
        try {
            while (flag) {
                condition_producer.await();
            }
            number++;
            System.out.println("生產者--------------" + number);
            flag = true;
            condition_consumer.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void destroy() {
        lock.lock();
        try {
            while (!flag) {
                condition_consumer.await();
            }
            System.out.println("消費者***" + number);
            flag = false;
            condition_producer.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}複製代碼
  • Producer.java
/** * Created by yuandl on 2016-10-12. * 生產者 */
public class Producer implements Runnable {
    private Resource resource;

    public Producer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.create();
        }
    }
}複製代碼
  • Consumer.java
/** * Created by yuandl on 2016-10-12. * 消費者 */
public class Consumer implements Runnable {
    private Resource resource;

    public Consumer(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource.destroy();
        }
    }
}複製代碼
  • MainTest.java
package thread.produceconsumer2;

/** * Created by yuandl on 2016-10-12. */
public class MainTest {
    public static void main(String args[]) {
        Resource resource = new Resource();
        new Thread(new Producer(resource)).start();
        new Thread(new Producer(resource)).start();
        new Thread(new Consumer(resource)).start();
        new Thread(new Consumer(resource)).start();
    }
}複製代碼
  • 打印結果
生產者--------------381
消費者***381
生產者--------------382
消費者***382
生產者--------------383
消費者***383
生產者--------------384
消費者***384
生產者--------------385
消費者***385
生產者--------------386
消費者***386
生產者--------------387
消費者***387
生產者--------------388
消費者***388
生產者--------------389
消費者***389
生產者--------------390
消費者***390
生產者--------------391
消費者***391複製代碼
相關文章
相關標籤/搜索