獻(xiàn)給母親節(jié)(object=>wait/notify)

本文屬于裝糊涂的豬原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處作者

背景交代:
??在上一篇OTA升級(jí)中有提到Nordic提供的升級(jí)庫(kù),看源碼時(shí)發(fā)現(xiàn)如下一些代碼:

private final Object mLock = new Object();
synchronized (mLock)
{ 
 mLock.notify();
}
synchronized (mLock) 
 {
 while ((mConnectionState == STATE_CONNECTING
|| mConnectionState ==STATE_CONNECTED) && mError == 0)
mLock.wait();
 }

心生疑惑,Object還能這么玩的???wait 、notify不是線程中的東東嗎?

mb.jpg

于是我找到官方的Object Class API,上面寫(xiě)道:

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
顯然是萬(wàn)類(lèi)之母啊,本周末就是母親節(jié)了,以本文獻(xiàn)祭給母親節(jié)了。


ty.gif

看下wait函數(shù)說(shuō)明(如下圖) :


waitapi.png

再看下notify函數(shù)說(shuō)明(如下圖) :


notify.jpg

歐了,大體的意思應(yīng)該是如下圖:
Wait and Notify Java Concurrency.jpg

于是擼出如下代碼:


public class LockTest01 {

    
    static Object lock = new Object();
    static boolean b =true;
    
    /**
     * @param args
     */
    public static void main(String[] args) {
            
        new Thread(){
            public void run() {
                synchronized (lock) {
                    while (b){
                        try {
                            System.out.println("wait start");
                            lock.wait();
                            try {
                                //do something
                                Thread.sleep(2000);
                                b=false;
                                System.out.println("wait  sleep  2s");
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println("wait end");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
        }.start();
        
        new Thread(){
            public void run() {
                synchronized (lock) {
                    System.out.println("notify start");
                    try {
                        //do something
                        Thread.sleep(2000);
                        System.out.println("notify  sleep  2s");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.notify();
                    System.out.println("notify end");
                }
            };
        }.start();
            
    }

}

Output:
wait start
notify start
notify  sleep  2s
notify end
wait  sleep  2s
wait end

但是這個(gè)究竟有什么應(yīng)用場(chǎng)景呢?
??比較典型的就是producer consumer problem,現(xiàn)在就以此為例:
??1)生產(chǎn)者線程每1秒生成一個(gè)新資源,并將其放入“taskQueue”中。
??2)消費(fèi)者線程需要1秒的時(shí)間來(lái)處理從“任務(wù)隊(duì)列”中消耗的資源。
??3)任務(wù)隊(duì)列的最大容量為5,在任何給定的時(shí)間內(nèi),可以在“任務(wù)隊(duì)列”中存在最多5個(gè)資源。
??4)兩個(gè)線程都是無(wú)限運(yùn)行的。

Producer Design

class Producer implements Runnable
{
   private final List<Integer> taskQueue;
   private final int           MAX_CAPACITY;
 
   public Producer(List<Integer> sharedQueue, int size)
   {
      this.taskQueue = sharedQueue;
      this.MAX_CAPACITY = size;
   }
 
   @Override
   public void run()
   {
      int counter = 0;
      while (true)
      {
         try
         {
            produce(counter++);
         } 
         catch (InterruptedException ex)
         {
            ex.printStackTrace();
         }
      }
   }
 
   private void produce(int i) throws InterruptedException
   {
      synchronized (taskQueue)
      {
         while (taskQueue.size() == MAX_CAPACITY)
         {
            System.out.println("Queue is full " + Thread.currentThread().getName() + " is waiting , size: " + taskQueue.size());
            taskQueue.wait();
         }
           
         Thread.sleep(1000);
         taskQueue.add(i);
         System.out.println("Produced: " + i);
         taskQueue.notifyAll();
      }
   }
}

Consumer Design

class Consumer implements Runnable
{
   private final List<Integer> taskQueue;
 
   public Consumer(List<Integer> sharedQueue)
   {
      this.taskQueue = sharedQueue;
   }
 
   @Override
   public void run()
   {
      while (true)
      {
         try
         {
            consume();
         } catch (InterruptedException ex)
         {
            ex.printStackTrace();
         }
      }
   }
 
   private void consume() throws InterruptedException
   {
      synchronized (taskQueue)
      {
         while (taskQueue.isEmpty())
         {
            System.out.println("Queue is empty " + Thread.currentThread().getName() + " is waiting , size: " + taskQueue.size());
            taskQueue.wait();
         }
         Thread.sleep(1000);
         int i = (Integer) taskQueue.remove(0);
         System.out.println("Consumed: " + i);
         taskQueue.notifyAll();
      }
   }
}

Application

public class ProducerConsumerExampleWithWaitAndNotify
{
   public static void main(String[] args)
   {
      List<Integer> taskQueue = new ArrayList<Integer>();
      int MAX_CAPACITY = 5;
      Thread tProducer = new Thread(new Producer(taskQueue, MAX_CAPACITY), "Producer");
      Thread tConsumer = new Thread(new Consumer(taskQueue), "Consumer");
      tProducer.start();
      tConsumer.start();
   }
}
 
Output:
 
Produced: 0
Produced: 1
Produced: 2
Produced: 3
Produced: 4
Queue is full Producer is waiting , size: 5
Consumed: 0
Consumed: 1
Consumed: 2
Consumed: 3
Consumed: 4
Queue is empty Consumer is waiting , size: 0
Produced: 5
Produced: 6
Produced: 7
Produced: 8
Produced: 9
Queue is full Producer is waiting , size: 5
Consumed: 5
Consumed: 6
Consumed: 7
Consumed: 8
Consumed: 9
Queue is empty Consumer is waiting , size: 0
```
下班寫(xiě)完這些,感覺(jué)自己
![pkq.jpg](http://upload-images.jianshu.io/upload_images/3876169-33620787f86e68b0.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Don't worry,be happy!
[傳送門(mén)(代碼地址)](https://github.com/ftc300/JavaLaboratory.git)以后做實(shí)驗(yàn)的java代碼repo
參考資料:
官方Object說(shuō)明文檔:
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html
stackoverflow :
http://stackoverflow.com/questions/886722/how-to-use-wait-and-notify-in-java
線程池 ExecutorService 執(zhí)行完畢 :
http://blog.csdn.net/truong/article/details/40227435
線程的wait/notify:
http://www.programcreek.com/2009/02/notify-and-wait-example/
how-to-work-with-wait-notify-and-notifyall-in-java :
http://howtodoinjava.com/core-java/multi-threading/how-to-work-with-wait-notify-and-notifyall-in-java/
![維護(hù)個(gè)公眾號(hào)試試吧.png](http://upload-images.jianshu.io/upload_images/3876169-5d1963a40559aa03.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,639評(píng)論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語(yǔ)法,類(lèi)相關(guān)的語(yǔ)法,內(nèi)部類(lèi)的語(yǔ)法,繼承相關(guān)的語(yǔ)法,異常的語(yǔ)法,線程的語(yǔ)...
    子非魚(yú)_t_閱讀 34,740評(píng)論 18 399
  • 1.解決信號(hào)量丟失和假喚醒 public class MyWaitNotify3{ MonitorObject m...
    Q羅閱讀 1,015評(píng)論 0 1
  • 旭小東閱讀 110評(píng)論 0 0
  • 在我的記憶中,不知什么時(shí)候年的意味開(kāi)始淡薄。尋常的日子,尋常地生活。在物質(zhì)漸漸豐裕的年代,每一天都是節(jié)日。每一天又...
    支點(diǎn)輕課class閱讀 452評(píng)論 0 0

友情鏈接更多精彩內(nèi)容