LockSupport对Thread.interrupt()的响应

下面我们来看下LockSupport对应中断的响应性

    public static void t2() throws Exception  
    {  
        Thread t = new Thread(new Runnable()  
        {  
            private int count = 0;  
      
            @Override  
            public void run()  
            {  
                long start = System.currentTimeMillis();  
                long end = 0;  
      
                while ((end - start) <= 1000)  
                {  
                    count++;  
                    end = System.currentTimeMillis();  
                }  
      
                System.out.println("after 1 second.count=" + count);  
      
            //等待或许许可  
                LockSupport.park();  
                System.out.println("thread over." + Thread.currentThread().isInterrupted());  
      
            }  
        });  
      
        t.start();  
      
        Thread.sleep(2000);  
      
        // 中断线程  
        t.interrupt();
          
        System.out.println("main over");  
    } 

最终线程会打印出thread over.true。这说明线程如果因为调用park而阻塞的话,能够响应中断请求(中断状态被设置成true),但是不会抛出InterruptedException

你可能感兴趣的:(JAVA)