Java | sleep、wait、yield、join、notify、notifyAll

62 阅读2分钟

sleep、wait、yield、join、notify、notifyAll

1. sleep

sleep(): 是Thread的静态方法 让调用线程进入睡眠状态,虽然线程睡眠了, 但是对象的锁没有释放,到时间会自动苏醒 , 线程变为阻塞状态;

    public static void main(String[] args) throws Exception{
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("线程 run运行 睡眠前 状态:"+Thread.currentThread().getState());
                    TimeUnit.SECONDS.sleep(10);
                    System.out.println("线程 run运行 苏醒后  状态:"+Thread.currentThread().getState());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        System.out.println("线程 start运行     状态:"+thread.getState());
        TimeUnit.SECONDS.sleep(1);
        System.out.println("线程 等待一秒后     状态:"+thread.getState());
    }

在这里插入图片描述

2. wait

wait(): 是Object 的方法, wait()方法在线程休眠的同时会释放掉锁,其他线程可以访问.需要其他线程调用notify 唤醒,wait 使用时必须先获取对象锁,必须在synchronized 修饰的代码块中使用.依赖锁,线程变为阻塞状态

public static void main(String[] args) throws Exception {
        Object o = new Object();

        //wait 线程
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (o) {
                        System.out.println(Thread.currentThread().getName() + "线程 获取到锁  wait前   状态:" + Thread.currentThread().getState());
                        o.wait();
                        System.out.println(Thread.currentThread().getName() + "线程 run运行 notify后  状态:" + Thread.currentThread().getState());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        threadA.start();
        System.out.println(threadA.getName() + "线程 start运行         状态:" + threadA.getState());
        TimeUnit.SECONDS.sleep(1);
        System.out.println(threadA.getName() + "线程 等待一秒后         状态:" + threadA.getState());

        //唤醒线程
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    synchronized (o) {
                        System.out.println(Thread.currentThread().getName() + "线程 获取到锁 notify前  状态:" + Thread.currentThread().getState());
                        o.notify();
                        System.out.println(Thread.currentThread().getName() + "线程 run运行 notify后  状态:" + Thread.currentThread().getState());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threadB.start();
    }

在这里插入图片描述

3. yield

yield(): 停止当前线程,线程重回就绪状态,不会让线程进入阻塞状态 让同等或者高于当前线程优先级的线程运行,低于当前线程优先级的线程 yield() 将不会起作用 也是Thread 的静态方法. 线程变为就绪状态;

4. join

join(): 使当前线程停下来等待 一直到另一个调用join方法的线程终止 阻塞状态

5. notify /notifyAll

notify():唤醒一个线程(唤醒那个线程取决于线程调度器) ,依赖锁

notifyAll(): 唤醒等待该锁的所有线程. ,依赖锁

如果对你有帮助,加个关注把~

在这里插入图片描述