Interrupted sleep blocking case in Java

Interrupted sleep blocking case in Java

public class Demo {
    
    
	public static void main(String[] args) {
    
    
		Thread g=new Thread(){
    
    
			public void run(){
    
    
				try {
    
    
					System.out.println("洗洗上床睡觉了");
					Thread.sleep(10000000);
				} catch (InterruptedException e) {
    
    
					System.out.println("诶呀。。。我的头好疼");
				}
				
			}
		};
		Thread x=new Thread(){
    
    
			public void run(){
    
    
				for(int i=0;i<5;i++){
    
    
					try {
    
    
						Thread.sleep(1000);
					} catch (InterruptedException e) {
    
    
					}
					System.out.println("开着车,唱着五环之歌 。。啊 啊 五~环。。你比四环多一环~~");
				}
				System.out.println("咣当,撞到师傅了");
				g.interrupt();
			}
		};
		g.start();
		x.start();
	}
}

Insert picture description here
In the above code, the sleep block is added to the thread g, but when the thread x ends, the g.interrupt() method is executed to interrupt the sleep block. Therefore, the thread g that should be blocked continues to execute after the thread x ends.

Guess you like

Origin blog.csdn.net/weixin_45772185/article/details/115230109