[英]How to stop thread in java, when it is hanged?
如何掛斷Java中的線程? 我不能使用interrupt(),因為我無法檢查掛起的線程中的中斷標志。 此外,不推薦使用stop(),因為它不安全。
提前致謝。
更新:線程在Socket.connect()上掛起:
Method m = device.getClass().getMethod("createInsecureRfcommSocket",
new Class[] { int.class });
final BluetoothSocket socket = (BluetoothSocket) m.invoke(device, port);
socket.connect();
將線程干凈地關閉的唯一方法是必須優雅地停止自己。
如果某個線程在Socket讀取或寫入等資源上被阻塞,您可以嘗試關閉它,它將觸發IOException。 如何解除阻塞線程完全取決於它被阻止的原因。
如果你強制一個線程停止,你已經放棄了它將被優雅地停止的想法,在這種情況下, Thread.stop()
或System.exit()
是你唯一的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.