sleep()和wait()区别

import lombok.*;

/*
o.wait();释放锁
    ...
Thread.sleep(6000);
    6004
    ...
* */
public class T {
    @SneakyThrows
    public static void main(String[] args) {
        Object o = new Object();
        new Thread(() -> {
            synchronized (o) {
                try {
                    long begin = System.currentTimeMillis();
                    Thread.sleep(6000);
                    // o.wait();
                    System.out.println(System.currentTimeMillis() - begin);

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

        new Thread(() -> {
            synchronized (o) {
                System.out.println("...");
            }
        }).start();
    }
}

posted @ 2024-08-17 15:04  干饭达人GoodLucy  阅读(3)  评论(0编辑  收藏  举报