两者对wait()中的纯种调用时意义相近,但也有差异:
1.notify()/notifyAll()是Object的方法,是该实例的等待区调用的,而不是对线程直接调用.notify()/notifyAll()所唤醒的线程,会前进到wait()的下一个语句继续执行.
2.interrupt()是Thread的方法,是对该线程直接调用.当被interrupt()的线程正在sleep或wait时,会抛出InterruptedException
3.前者要获取类实例的锁定,后者直接取消其他线程,不需要获取锁定.
顺便加个与Thread.sleep()相同效果的代码:
public static void amethod(long x) throws InterruptedExcetion{
if (x != 0) {
Object o = new Object();
synchronized (o) {
o.wait(x);
}
}
}
分享到:
相关推荐
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM) app := von.New(shutdown) app.Handle(http.MethodGet, "/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { data := ma
add download interrupt hint v 0.7.5 fix bugs v 0.7.4 add delay on changing panel v 0.7.1 add download start notify v 0.6.1 add download progress, download notify v 0.5.1 add context menu v 0.4.1 add ...
│ 高并发编程第一阶段15讲、Thread中断Interrupt方法详细讲解.mp4 │ 高并发编程第一阶段16讲、采用优雅的方式结束线程生命周期.mp4 │ 高并发编程第一阶段17讲、Thread API综合实战,编写ThreadService实现暴力...
│ 高并发编程第一阶段15讲、Thread中断Interrupt方法详细讲解.mp4 │ 高并发编程第一阶段16讲、采用优雅的方式结束线程生命周期.mp4 │ 高并发编程第一阶段17讲、Thread API综合实战,编写ThreadService实现暴力...
exclusion and the interrupt level indication flag is not needed. Fixed a problem with the Global Lock where the lock could appear to be obtained before it is actually obtained. The global lock ...
_URB_BULK_OR_INTERRUPT_TRANSFER _URB_CONTROL_DESCRIPTOR_REQUEST _URB_CONTROL_FEATURE_REQUEST _URB_CONTROL_GET_CONFIGURATION_REQUEST _URB_CONTROL_GET_INTERFACE_REQUEST _URB_CONTROL_GET_STATUS_...
启点CE过NP中文December 24 2018:Cheat Engine 6.8.2 Released: Here's a new version for the hollidays. Mainly minor improvements and some small bugfixes, but also a new 'ultimap like' feature called ...
25 JAVA8 与元数据.................................................................................................................................25 2.4. 垃圾回收与算法 .................................
25 JAVA8 与元数据.................................................................................................................................25 2.4. 垃圾回收与算法 .................................
What’s Inside Preface 1 Java SE5 and SE6 .................. 2 Java SE6 ............................................The 4th edition...........................Changes ...........................................