Thread interrupt 方法

    我们在看一些多线程代码的时候,有的时候会碰到使用interrupt()方法的时候。从字面的意思来理解,应该就是中断当前正在执行的线程。那么,对于一个我们设计的普通线程来说,如果我们在主线程里调用它的interrupt()方法,会不会导致它被中断呢?

    比如说我们先写一段如下的代码:

Java代码 复制代码  收藏代码
  1. import java.util.concurrent.TimeUnit;   
  2.   
  3. public class ThreadCon extends Thread   
  4. {   
  5.     public void run()   
  6.     {   
  7.           
  8.         for(int i = 0; i < Integer.MAX_VALUE; i++)   
  9.         {   
  10.             System.out.println(i);   
  11.         }   
  12.            
  13.     }   
  14.   
  15.     public static void main(String[] args)   
  16.     {   
  17.         ThreadCon thread = new ThreadCon();   
  18.         thread.start();   
  19.         System.out.println("main thread");   
  20.         try    
  21.         {   
  22.             TimeUnit.SECONDS.sleep(2);   
  23.         }    
  24.         catch(InterruptedException e)    
  25.         {   
  26.             e.printStackTrace();   
  27.         }   
  28.         thread.interrupt();   
  29.     }   
  30. }  
import java.util.concurrent.TimeUnit;

public class ThreadCon extends Thread
{
    public void run()
    {
       
        for(int i = 0; i < Integer.MAX_VALUE; i++)
        {
            System.out.println(i);
        }
        
    }

    public static void main(String[] args)
    {
        ThreadCon thread = new ThreadCon();
        thread.start();
        System.out.println("main thread");
        try 
        {
            TimeUnit.SECONDS.sleep(2);
        } 
        catch(InterruptedException e) 
        {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}

    这是一段比较简单的示例,我们在开始的时候启动一个子线程,这个线程打印从0到Integer.MAX_VALUE 的值。而主线程先sleep 2秒钟。然后再尝试去中断子线程。

    如果我们去运行前面这一段代码,会发现子线程会一直在输出数字结果,它根本就不会停下来。部分结果的输出如下:

Java代码 复制代码  收藏代码
  1. 141318  
  2. 141319  
  3. 141320  
  4. 141321  
  5. 141322  
  6. 141323  
  7. 141324  
  8. 141325  
141318
141319
141320
141321
141322
141323
141324
141325

     由此可见,我们对一个子线程发interrupt的消息时,如果线程是在运行的状态之下,它会忽略这个请求而继续执行的。

    那么,在什么情况下才会导致这个触发的interrupt消息让子线程能够响应呢?

interrupt方法处理方式

    interrupt方法调用的时候会设置该线程的interrupt标识位(flag),相当于设置了一个状态信息。执行的子线程需要检查该标识位是否被设置了然后才能做后续的执行步骤。这个检查的方法就是isInterrupted()。这样,在一旦检测到这个位被设置之后,我们就可以采取对应的措施。

    我们在前面的代码的基础上做一点修改:

Java代码 复制代码  收藏代码
  1. import java.util.concurrent.TimeUnit;   
  2.   
  3. public class ThreadCon extends Thread   
  4. {   
  5.     public void run()   
  6.     {   
  7.           
  8.         for(int i = 0; i < Integer.MAX_VALUE; i++)   
  9.         {   
  10.             System.out.println(i);   
  11.             if(isInterrupted())   
  12.             {   
  13.                 System.out.println("Interrupted...");   
  14.                 return;   
  15.             }   
  16.         }   
  17.            
  18.     }   
  19.   
  20.     public static void main(String[] args)   
  21.     {   
  22.         ThreadCon thread = new ThreadCon();   
  23.         thread.start();   
  24.         System.out.println("main thread");   
  25.         try    
  26.         {   
  27.             TimeUnit.SECONDS.sleep(2);   
  28.         }    
  29.         catch(InterruptedException e)    
  30.         {   
  31.             e.printStackTrace();   
  32.         }   
  33.         thread.interrupt();   
  34.     }   
  35. }  
import java.util.concurrent.TimeUnit;

public class ThreadCon extends Thread
{
    public void run()
    {
       
        for(int i = 0; i < Integer.MAX_VALUE; i++)
        {
            System.out.println(i);
            if(isInterrupted())
            {
                System.out.println("Interrupted...");
                return;
            }
        }
        
    }

    public static void main(String[] args)
    {
        ThreadCon thread = new ThreadCon();
        thread.start();
        System.out.println("main thread");
        try 
        {
            TimeUnit.SECONDS.sleep(2);
        } 
        catch(InterruptedException e) 
        {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}

     这里,我们在run方法的循环里增加了一个判断,调用isInterrupted方法。如果被设置为interrupted,则显示一个信息然后退出。这个时候如果我们再执行这部分代码,会发现结果如下:

Java代码 复制代码  收藏代码
  1. 141370  
  2. 141371  
  3. 141372  
  4. 141373  
  5. 141374  
  6. 141375  
  7. 141376  
  8. 141377  
  9. 141378  
  10. Interrupted...  
141370
141371
141372
141373
141374
141375
141376
141377
141378
Interrupted...

     所以说,从前面的代码中可以看到,这是一种处理interrupt方法的手段。在实际的一些应用中,我们可以通过抛出异常然后由调用程序捕捉的方式来处理。比如在前面的这个示例里,我们判断了isInterrupted了之后就在接着的部分处理了。如果我们希望由调用该方法的使用者来处理。可以在这里不做进一步的处理而是直接写一个throw new InterruptedException()。

这里也有一个稍微复杂点的示例,通过遍历文件系统,然后在处理的子方法里面来抛出异常,再由调用的方法来处理。

Java代码 复制代码  收藏代码
  1. import java.io.File;   
  2.   
  3. public class FileSearch implements Runnable {   
  4.     private String initPath;   
  5.     private String fileName;   
  6.        
  7.     public FileSearch(String initPath, String fileName) {   
  8.             this.initPath = initPath;   
  9.             this.fileName = fileName;   
  10.     }   
  11.        
  12.     @Override  
  13.     public void run() {   
  14.         File file = new File(initPath);   
  15.         if(file.isDirectory()) {   
  16.             try {   
  17.                 directoryProcess(file);   
  18.             } catch(InterruptedException e) {   
  19.                 System.out.printf("%s: The search has been interrupted",    
  20.                         Thread.currentThread().getName());   
  21.             }   
  22.         }   
  23.     }   
  24.        
  25.     private void directoryProcess(File file) throws InterruptedException {   
  26.         File[] list = file.listFiles();   
  27.         if(list != null) {   
  28.             for(int i = 0; i < list.length; i++) {   
  29.                 if(list[i].isDirectory()) {   
  30.                     directoryProcess(list[i]);   
  31.                 } else {   
  32.                     fileProcess(list[i]);   
  33.                 }   
  34.             }   
  35.         }   
  36.         if(Thread.interrupted()) {   
  37.             throw new InterruptedException();   
  38.         }   
  39.     }   
  40.        
  41.     private void fileProcess(File file) throws InterruptedException {   
  42.         if(file.getName().equals(fileName)) {   
  43.             System.out.printf("%s : %s\n", Thread.currentThread().getName(),   
  44.                     file.getAbsolutePath());   
  45.         }   
  46.         if(Thread.interrupted()) {   
  47.             throw new InterruptedException();   
  48.         }   
  49.     }   
  50. }  
import java.io.File;

public class FileSearch implements Runnable {
	private String initPath;
	private String fileName;
	
	public FileSearch(String initPath, String fileName) {
			this.initPath = initPath;
			this.fileName = fileName;
	}
	
	@Override
	public void run() {
		File file = new File(initPath);
		if(file.isDirectory()) {
			try {
				directoryProcess(file);
			} catch(InterruptedException e) {
				System.out.printf("%s: The search has been interrupted", 
						Thread.currentThread().getName());
			}
		}
	}
	
	private void directoryProcess(File file) throws InterruptedException {
		File[] list = file.listFiles();
		if(list != null) {
			for(int i = 0; i < list.length; i++) {
				if(list[i].isDirectory()) {
					directoryProcess(list[i]);
				} else {
					fileProcess(list[i]);
				}
			}
		}
		if(Thread.interrupted()) {
			throw new InterruptedException();
		}
	}
	
	private void fileProcess(File file) throws InterruptedException {
		if(file.getName().equals(fileName)) {
			System.out.printf("%s : %s\n", Thread.currentThread().getName(),
					file.getAbsolutePath());
		}
		if(Thread.interrupted()) {
			throw new InterruptedException();
		}
	}
}

 启用该线程的部分代码如下:

Java代码 复制代码  收藏代码
  1. import java.util.concurrent.TimeUnit;   
  2.   
  3. /**  
  4.  *  Main class of the example. Search for the autoexect.bat file  
  5.  *  on the Windows root folder and its subfolders during ten seconds  
  6.  *  and then, interrupts the Thread  
  7.  */  
  8. public class Main {   
  9.   
  10.     /**  
  11.      * Main method of the core. Search for the autoexect.bat file  
  12.      * on the Windows root folder and its subfolders during ten seconds  
  13.      * and then, interrupts the Thread  
  14.      * @param args  
  15.      */  
  16.     public static void main(String[] args) {   
  17.         // Creates the Runnable object and the Thread to run it   
  18.         FileSearch searcher=new FileSearch("C:\\","autoexec.bat");   
  19.         Thread thread=new Thread(searcher);   
  20.            
  21.         // Starts the Thread   
  22.         thread.start();   
  23.            
  24.         // Wait for ten seconds   
  25.         try {   
  26.             TimeUnit.SECONDS.sleep(10);   
  27.         } catch (InterruptedException e) {   
  28.             e.printStackTrace();   
  29.         }   
  30.            
  31.         // Interrupts the thread   
  32.         thread.interrupt();   
  33.     }   
  34.   
  35. }  
import java.util.concurrent.TimeUnit;

/**
 *  Main class of the example. Search for the autoexect.bat file
 *  on the Windows root folder and its subfolders during ten seconds
 *  and then, interrupts the Thread
 */
public class Main {

	/**
	 * Main method of the core. Search for the autoexect.bat file
	 * on the Windows root folder and its subfolders during ten seconds
	 * and then, interrupts the Thread
	 * @param args
	 */
	public static void main(String[] args) {
		// Creates the Runnable object and the Thread to run it
		FileSearch searcher=new FileSearch("C:\\","autoexec.bat");
		Thread thread=new Thread(searcher);
		
		// Starts the Thread
		thread.start();
		
		// Wait for ten seconds
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		// Interrupts the thread
		thread.interrupt();
	}

}

 

总结

    interrupt方法的效果是它会设置线程的状态位,但是对于正在运行状态的线程并不会导致它被立即中断。只有执行的线程在检测到相关信息后采取对应措施。我们常用的措施一般是调用isInterrupted方法或者捕捉被调用方法抛出的InterruptedException来做处理。整体的应用并不复杂。

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值