Java - 阻塞和唤醒线程的方法

使用 Thread.sleep() 方法,会阻塞当前线程,但是在阻塞用户设置的时间后自动进入就绪态,在此期间并不会释放已经获得的同步锁

一. Object 类 提供的方法 wait & notify

  1. 使用方式

public class WaitAndNotify {
    public static void main(String[] args) {
        // 设置对象锁
        Object o =new Object();
        new Thread(()->{
            synchronized (o){
                try {
                    System.out.println(Thread.currentThread().getName() + "\t-----阻塞当前线程");
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t-----被唤醒");
            }
        },"t1").start();

        new Thread(()->{
            synchronized (o){
                o.notify();
                System.out.println(Thread.currentThread().getName() + "\t-----发出唤醒通知");
            }
        },"t2").start();

    }
}

 2. 执行结果: 

 3. 注意:

       wait 方法将当前持有该锁的线程阻塞直至被 notify 唤醒, wait 和 notify 方法只能写在同步代码块里面

二. Lock + Condition 配合:await & signal

 1. 使用方式

public class LockAndCondition {
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(()->{
           lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "\t-----come in");
                condition.await();
                System.out.println(Thread.currentThread().getName() + "\t-----被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        },"t1").start();

        try {TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}

        new Thread(()->{
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + "\t-----come in");
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "\t-----发出唤醒通知");
            }finally {
                lock.unlock();
            }
        },"t2").start();
    }
}

 2. 运行结果:

 3. 注意:

        await 和 signal 方法也必须写在 lock 和 unlock 同步方法块中

三. LockSupport 中的 park 和 unpark 方法

1. 使用方式

public class ParkAndUnpark {

    public static void main(String[] args) {

        Thread t1 = new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "\t-----come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "\t-----被唤醒");
        },"t1");
        t1.start();

        try { TimeUnit.MILLISECONDS.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}

        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "\t-----come in");
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "\t-----发出唤醒通知");
        },"t2").start();
    }
}

2. 执行结果

 3. 注意

        1)不需要写在同步代码块中,成对出现

        2)支持先 unpark 获得通行证,park 并不会进行阻塞

        3)通过unpark提前获得的通行证,最多只有一个,重复多个只有一个生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值