interrupt()会中断线程的wait等待
生活随笔
收集整理的這篇文章主要介紹了
interrupt()会中断线程的wait等待
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class Thread5 {public static void main(String[] args) {SubThread subThread = new SubThread();subThread.start();try {//主線程睡眠2秒,確保子線程處于wait狀態Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}subThread.interrupt();}private static final Object lock = new Object();static class SubThread extends Thread {@Overridepublic void run() {synchronized (lock) {System.out.println("begin wait");try {lock.wait();System.out.println("end wait");} catch (InterruptedException e) {System.out.println("等待被中斷");e.printStackTrace();}}}}
}
總結
以上是生活随笔為你收集整理的interrupt()会中断线程的wait等待的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 友盟分享之微信分享
- 下一篇: java BigDecimal去掉小数点