发布于2023-04-25 阅读(0)
扫一扫,手机访问
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; } } } }
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店