1、解决办法
(1)使用java线程时,将经常使用wait方法,并且如果在调用wait方法时中断了,jvm将捕获该中断,并持续调用wait指令。
(2)此时即使使用interrupt发送法中断,也不会发生任何效果。
(3)wait方法需要进行一些封装,捕获异常,然后停止执行该异常。
2、实例
public static void wait(object obj) { boolean interrupted = true; while (interrupted) { interrupted = false; try { obj.wait(); } catch (interruptedexception e) { interrupted = true; } } } public static void wait(object obj, int timeout) { boolean interrupted = true; long starttime = system.currenttimemillis(); int sleeptimeout = timeout; while (interrupted) { interrupted = false; try { obj.wait(sleeptimeout); } catch (interruptedexception e) { interrupted = true; long now = system.currenttimemillis(); sleeptimeout -= now - starttime; starttime = now; if (sleeptimeout < 0) { interrupted = false; } } }}
以上就是java中wait调用中断怎么解决的详细内容。