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呼叫中斷怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!