Thread wakeup mechanism

1. Wait and notify implement waiting and wake-up

To be used in a synchronized lock, wait before waking up

        Object object = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (object) {
                try {
                    System.out.println("================进入线程=============");
                    object.wait();
                    System.out.println("================线程被唤醒=============");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t1");
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() ->{
            synchronized (object){
                object.notify();
                System.out.println("进行唤醒==============");
            }
        },"t2").start();

2. await and signal realize waiting and waking up

It needs to be used under lock, and it needs to be implemented with condition, and it can be woken up after waiting first.

        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        Thread t1 = new Thread(() -> {
            lock.lock();
            try{
                System.out.println("=============进入方法==============");
                condition.await();
                System.out.println("=================线程被唤醒=================");
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }, "t1");
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() ->{
            lock.lock();
            try{
                condition.signal();
                System.out.println("=============唤醒操作================");
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        },"t2").start();

3. Park and unpark implement waiting and wake-up

Unpark releases licenses: at most one license can be issued (not cumulative). If a method appears twice LockSupport.park(); there will be a problem.

Based on the implementation of LockSupport, there is no order requirement, you can park or unpark first. The bottom layer is implemented based on the UNSAFE class

        Thread t1 = new Thread(() -> {
            System.out.println("===============进入线程执行=====================");
            LockSupport.park();
            System.out.println("===============线程被唤醒=====================");
        }, "t1");
        t1.start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(() ->{
            LockSupport.unpark(t1);
            System.out.println("================进行唤醒==================");
        },"t2").start();

Guess you like

Origin blog.csdn.net/qq_35771266/article/details/126074164