wait/notify(*)

使用wait/notify 方法實(shí)現(xiàn)線程間的通信。(注意著兩個(gè)方法都是object的類的方法,換句話說(shuō)java為所有的對(duì)象都提供了這兩個(gè)方法)

1.wait 和 notify 必須配合synchronized關(guān)鍵字使用。
2. wait方法釋放鎖,notify方法不釋放鎖。

package jvmtest;
import java.util.ArrayList;
import java.util.List;
public class ListAdd1 {
    private volatile static List list = new ArrayList();
    
    public void add() {
        list.add("bjsxt");
    }
    
    public int size() {
        return list.size();
    }
    
    
    public static void main(String[] args) {
        final ListAdd1 list1 = new ListAdd1();
        
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10; i++) {
                        list1.add();
                        System.out.println("當(dāng)前線程:"+Thread.currentThread().getName());
                        Thread.sleep(500);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t1");
        
        
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    if(list1.size() == 5) {
                        System.out.println("當(dāng)前線程收到通知:"+Thread.currentThread().getName()+"list size = 5 線程停止..");
                        throw new RuntimeException();
                    }
                }
            }
        },"t2");

        t1.start();
        t2.start();
    }
}

結(jié)果如下:

當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程收到通知:t2list size = 5 線程停止..
Exception in thread "t2" java.lang.RuntimeException
at jvmtest.ListAdd1$2.run(ListAdd1.java:44)
at java.lang.Thread.run(Thread.java:745)
當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程:t1
當(dāng)前線程:t1

線程2不斷的死循環(huán)判斷條件是否達(dá)成,該方式十分消耗性能,不建議采用

wait 和 notify 方式實(shí)現(xiàn)

  package jvmtest;

import java.util.ArrayList;
import java.util.List;

/**
 * wait notify 方法,wait釋放鎖 , notify 不釋放鎖
 * 
 * @author zhouhai
 *
 *         2018年10月15日
 */
public class ListAdd2 {

    private volatile static List list = new ArrayList();

    public void add() {
        list.add("bjsxt");
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        final ListAdd2 list2 = new ListAdd2();

        // 1.實(shí)例化出來(lái)一個(gè)lock
        // 當(dāng)使用wait 和 notify 的時(shí)候,一定要配合
        final Object lock = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (lock) {
                        for (int i = 0; i < 10; i++) {
                            list2.add();
                            System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "添加了一個(gè)元素...");
                            Thread.sleep(500);
                            if (list2.size() == 5) {
                                System.out.println("已經(jīng)發(fā)出通知..");
                                lock.notify();
                            }
                        }

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        },"t1");
        
        
        
        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                synchronized(lock)
                {
                    if(list2.size() != 5) {
                        try {
                            lock.wait();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("當(dāng)前線程:"+Thread.currentThread().getName()+"收到通知線程停止...");
                    throw new RuntimeException();
                }
            }
        },"t2");
        
        t2.start();
        t1.start();
        
    }

}

結(jié)果如下:

當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
已經(jīng)發(fā)出通知..
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t2收到通知線程停止...
Exception in thread "t2" java.lang.RuntimeException
    at jvmtest.ListAdd2$2.run(ListAdd2.java:72)
    at java.lang.Thread.run(Thread.java:745)```

`wait/notify方式的弊端:由于notify不釋放鎖,線程1要執(zhí)行完所有的遍歷,才會(huì)釋放鎖,如果遍歷100萬(wàn)次會(huì)十分消耗性能,并且會(huì)存在不實(shí)時(shí)的問(wèn)題`

通過(guò) countDownLatch類解決不實(shí)時(shí)的問(wèn)題

package jvmtest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
 * 
 * countDownLatch類可實(shí)時(shí)通知
 * @author zhouhai
 *
 *         2018年10月15日
 */
public class ListAdd2 {

    private volatile static List list = new ArrayList();

    public void add() {
        list.add("bjsxt");
    }

    public int size() {
        return list.size();
    }

    public static void main(String[] args) {
        final ListAdd2 list2 = new ListAdd2();

        //1表示一次countDown通知
        final CountDownLatch countDownLatch = new CountDownLatch(1);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10; i++) {
                        list2.add();
                        System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "添加了一個(gè)元素...");
                        Thread.sleep(500);
                        if (list2.size() == 5) {
                            System.out.println("已經(jīng)發(fā)出通知..");
                            countDownLatch.countDown();
                        }
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }, "t1");

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                if (list2.size() != 5) {
                    try {
                        countDownLatch.await();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("當(dāng)前線程:" + Thread.currentThread().getName() + "收到通知線程停止...");
                throw new RuntimeException();
            }
        }, "t2");

        t2.start();
        t1.start();

    }

}

結(jié)果如下:

當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
已經(jīng)發(fā)出通知..
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t2收到通知線程停止...
Exception in thread "t2" java.lang.RuntimeException
    at jvmtest.ListAdd2$2.run(ListAdd2.java:65)
    at java.lang.Thread.run(Thread.java:745)
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...
當(dāng)前線程:t1添加了一個(gè)元素...

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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