zoukankan      html  css  js  c++  java
  • Object的wait和Thread的sleep

    Object的wait()

    wait()搭配notify(),nofityAll()使用。
    线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notify()后,线程重新开始运行。

    final static Object async = new Object();
    
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (async) {
                        System.out.println("thread a get async");
                        try {
                            System.out.println("thread a start wait");
                            async.wait();
                            System.out.println("thread a end wait");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (async){
                        System.out.println("thread b get async");
                        System.out.println("thread b notify async");
                        async.notify();
                    }
                }
            }).start();
        }
    

    输出:

    thread a get async
    thread a start wait
    thread b get async
    thread b notify async
    thread a end wait
    

    Thread.sleep()

    线程获取到对象锁之后,sleep时不会释放对象锁,其他线程也不能获取到对象锁。直到线程sleep结束,其他线程才能获取到对象锁。

    final static Object async = new Object();
    
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (async) {
                        System.out.println("thread a get async");
                        try {
                            System.out.println("thread a start sleep");
                            Thread.sleep(1000);
                            System.out.println("thread a end sleep");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (async) {
                        System.out.println("thread b get async");
                        System.out.println("thread b notify async");
                        async.notify();
                    }
                }
            }).start();
        }
    

    输出:

    thread a get async
    thread a start sleep
    thread a end sleep
    thread b get async
    thread b notify async
    
  • 相关阅读:
    【3】hexo+github搭建个人博客的主题配置
    【2】hexo+github搭建个人博客的简单使用
    每日思考(2020/05/06)
    每日思考(2020/05/05)
    每日思考(2020/03/27)
    文件和异常
    每日思考(2020/03/24)
    图形用户界面和游戏开发
    每日思考(2020/03/19)
    面向对象进阶
  • 原文地址:https://www.cnblogs.com/jiy-for-you/p/7282042.html
Copyright © 2011-2022 走看看