首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

JDK源码分析-Lock&Condition

阻塞式获取锁,该方法与synchronized功能类似 void lock(); // 获取锁,可响应中断 void lockInterruptibly() throws InterruptedException...// 尝试获取锁(在给定的时间内),若成功返回true;否则返回false boolean tryLock(long time, TimeUnit unit) throws InterruptedException...Condition 接口定义如下: public interface Condition { // 使当前线程等待,直到被signal唤醒或被中断 void await() throws InterruptedException...// 使当前线程等待,直到被signal唤醒、或被中断、或到达等待时间(与上面方法类似) boolean await(long time, TimeUnit unit) throws InterruptedException...; // // 使当前线程等待,直到被signal唤醒、或被中断、或到达给定的截止时间 boolean awaitUntil(Date deadline) throws InterruptedException

31610

我们该如何正确的中断一个正在执行的线程??

写在前面 当我们在调用Java对象的wait()方法或者线程的sleep()方法时,需要捕获并处理InterruptedException异常。...如果我们对InterruptedException异常处理不当,则会发生我们意想不到的后果!今天,我们就以一个案例的形式,来为大家详细介绍下为何中断执行的线程不起作用。...currentThread.interrupt(); 这就使得我们捕获到InterruptedException异常后,能够重新设置线程的中断标志位,从而中断当前执行的线程。...总结 处理InterruptedException异常时要小心,如果在调用执行线程的interrupt()方法中断执行线程时,抛出了InterruptedException异常,则在触发InterruptedException...此时,正确的处理方式是在执行线程的run()方法中捕获到InterruptedException异常,并重新设置中断标志位(也就是在捕获InterruptedException异常的catch代码块中,

70620
领券