java sleep wait 区别

sleep是线程方法,wait是Object方法
sleep 不会释放锁, wait会释放锁
sleep不依赖同步方法,wait需要
sleep不需要被唤醒,wait(不给 long timeout 这个参数,则需要唤醒)需要

  1. sleep是线程方法,wait是Object方法
    查看java api可以看出
  2. sleep 会释放锁, wait不会释放锁
    新建 Test01
/**
 * 测试 sleep 会释放锁, wait不会释放锁
 * 测试sleep
 */
public class Test01 {
    private static String lock = "lock";

    public static void main(String[] args) {
        Test01 test01 = new Test01();
        test01.test01();
        test01.test02();
    }

    /**
     * 线程01
     */
    public void test01(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    try {
                        String name = Thread.currentThread().getName();
                        System.out.println("该线程 " + name + " 开始执行");
                        Thread.sleep(1000);
                        System.out.println("该线程 " + name + " 结束执行");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "线程01");
        thread.start();
    }

    /**
     * 线程02
     */
    public void test02(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    try {
                        String name = Thread.currentThread().getName();
                        System.out.println("该线程 " + name + " 开始执行");
                        Thread.sleep(1000);
                        System.out.println("该线程 " + name + " 结束执行");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "线程02");
        thread.start();
    }


}

java sleep 和wait javasleep和wait区别_ide


可以看出,sleep不会释放锁,只有执行结束之后,第二个线程才能获取锁

新建 Test02

/**
 * 测试 sleep 会释放锁, wait不会释放锁
 * 测试wait
 */
public class Test02 {
    private static String lock = "lock";

    public static void main(String[] args) {
        Test02 test02 = new Test02();
        test02.test01();
        test02.test02();
    }

    /**
     * 线程01
     */
    public void test01(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    try {
                        String name = Thread.currentThread().getName();
                        System.out.println("该线程 " + name + " 开始执行");
                        lock.wait(1000);
                        System.out.println("该线程 " + name + " 结束执行");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "线程01");
        thread.start();
    }

    /**
     * 线程02
     */
    public void test02(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    try {
                        String name = Thread.currentThread().getName();
                        System.out.println("该线程 " + name + " 开始执行");
                        lock.wait(1000);
                        System.out.println("该线程 " + name + " 结束执行");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "线程02");
        thread.start();
    }


}

java sleep 和wait javasleep和wait区别_同步方法_02


当线程01 wait,线程02获取了锁

因此wait会释放锁

  1. sleep不依赖同步方法,wait需要
    新建 Test03
/**
 * 测试 sleep不依赖同步方法,wait需要
 * 测试 是否需要 synchronized
 */
public class Test03 {
    private static String lock = "lock";

    public static void main(String[] args) {
        Test03 test03 = new Test03();
        test03.test01();
        test03.test02();
    }

    /**
     * 线程01
     */
    public void test01(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String name = Thread.currentThread().getName();
                    System.out.println("该线程 " + name + " 开始执行");
                    Thread.sleep(1000);
                    System.out.println("该线程 " + name + " 结束执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "线程01");
        thread.start();
    }

    /**
     * 线程02
     */
    public void test02(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String name = Thread.currentThread().getName();
                    System.out.println("该线程 " + name + " 开始执行");
                    lock.wait(1000);
                    System.out.println("该线程 " + name + " 结束执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "线程02");
        thread.start();
    }


}

java sleep 和wait javasleep和wait区别_java sleep 和wait_03


这个异常会在三种情况下抛出:

1>当前线程不含有当前对象的锁资源的时候,调用obj.wait()方法;

2>当前线程不含有当前对象的锁资源的时候,调用obj.notify()方法。

3>当前线程不含有当前对象的锁资源的时候,调用obj.notifyAll()方法。

  1. sleep不需要被唤醒,wait(不给 long timeout 这个参数,则需要唤醒)需要
    新建 Test04
/**
 * 测试 sleep不依赖同步方法,wait需要
 * 测试 是否需要 synchronized
 */
public class Test04 {
    private static String lock = "lock";

    public static void main(String[] args) {
        Test04 test04 = new Test04();
        test04.test01();
        test04.test02();
    }

    /**
     * 线程01
     */
    public void test01(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String name = Thread.currentThread().getName();
                    System.out.println("该线程 " + name + " 开始执行");
                    Thread.sleep(1000);
                    System.out.println("该线程 " + name + " 结束执行");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "线程01");
        thread.start();
    }

    /**
     * 线程02
     */
    public void test02(){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    try {
                        String name = Thread.currentThread().getName();
                        System.out.println("该线程 " + name + " 开始执行");
                        lock.wait();
                        System.out.println("该线程 " + name + " 结束执行");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "线程02");
        thread.start();
    }


}

java sleep 和wait javasleep和wait区别_java sleep 和wait_04


sleep 时间到了 则会继续执行,而 wait()是需要唤醒的,如果不唤醒,则会等待到天荒与地老