重學JAVA基礎(七):線程的wait、notify、notifyAll、sleep

來源:互聯網
上載者:User

標籤:

/** * 測試thread的wait  notify  notifyAll  sleep  Interrupted * @author tomsnail * @date 2015年4月20日 下午3:20:44 */public class Test1 {        /**     * 對象鎖     * @author tomsnail     * @date 2015年4月20日 下午3:14:13     */    private static final Object lockObject = new Object();        /**     * 等待線程     * @author tomsnail     * @date 2015年4月20日 下午3:14:22     */    static class Thread1 implements Runnable{        @Override        public void run() {            synchronized (lockObject) {                try {                    System.out.println(Thread.currentThread().getName()+"wait start");                    lockObject.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println(Thread.currentThread().getName()+"wait end");            }        }            }        /**     * 喚醒線程     * @author tomsnail     * @date 2015年4月20日 下午3:14:36     */    static class Thread2 implements Runnable{        @Override        public void run() {            synchronized (lockObject) {                lockObject.notify();                System.out.println(Thread.currentThread().getName()+"notify");            }        }    }        /**     * 喚醒所有線程     * @author tomsnail     * @date 2015年4月20日 下午3:14:51     */    static class Thread3 implements Runnable{        @Override        public void run() {            synchronized (lockObject) {                lockObject.notifyAll();                System.out.println(Thread.currentThread().getName()+"notifyAll");            }        }    }        /**     * 睡眠線程     * @author tomsnail     * @date 2015年4月20日 下午3:20:30     */    static class Thread4 implements Runnable{        @Override        public void run() {            try {                System.out.println(Thread.currentThread().getName()+"sleep");                Thread.currentThread().sleep(20000);            } catch (InterruptedException e) {                System.out.println(Thread.currentThread().getName()+"Interrupted");            }                    }            }    public static void main(String[] args) {        Thread t1 = new Thread(new Thread1());        Thread t3 = new Thread(new Thread1());        Thread t4 = new Thread(new Thread1());        Thread t2 = new Thread(new Thread2());        Thread t5 = new Thread(new Thread3());        //3個等待線程運行        t1.start();        t3.start();        t4.start();        try {            Thread.currentThread().sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }        //喚醒線程運行        t2.start();        try {            Thread.currentThread().sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }        //喚醒所有線程運行        t5.start();        //睡眠線程        Thread t6 = new Thread(new Thread4());        t6.start();            try {            Thread.currentThread().sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }        //睡眠線程中斷        t6.interrupt();    }}

結果

Thread-0wait startThread-2wait startThread-1wait startThread-3notifyThread-0wait endThread-4notifyAllThread-1wait endThread-2wait endThread-5sleepThread-5Interrupted

 

重學JAVA基礎(七):線程的wait、notify、notifyAll、sleep

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.