Java7并发示例集104:可控的线程中断
在上一节“线程中断”中,我们讲解了如何中断一个正在执行的线程以及为了中断线程,我们必须对Thread
动点什么手脚。一般情况下,我们可以使用上一节介绍的中断机制。但是,如果线程实现了一个分配到多个方法中的复杂算法,或者方法调用中有一个递归调用,我们应该使用更好的方式来控制线程的中断。为此,Java提供了InterruptedException
异常。当检测到中断请求时,可以抛出此异常,并且在run()
方法中捕获。
在本节,我们将使用一个线程查找指定目录及其子目录下文件来演示通过使用InterruptedException
异常控制线程中断。
知其然
按照下面所示步骤,实现示例程序。
-
创建一个名为FileSearch
的类,并且实现Runnable
接口。代码如下:
1 | public class FileSearch implements Runnable { |
-
声明两个变量,一个用于需要查找的文件名,一个用于初始化查找的目录;实现类的构造函数,并用构造函数的参数初始化刚刚声明的两个变量。代码如下:
1 | private String initPath; |
2 | private String fileName; |
4 | public FileSearch(String initPath, String fileName) { |
5 | this .initPath = initPath; |
6 | this .fileName = fileName; |
-
实现run()
方法,该方法检查fileName
是否一个路径名称。如果是,则调用directoryProcess()
方法进行处理。directoryProcess()
方法会抛出InterruptedException
异常,所以我们需要捕获该异常。代码如下:
03 | File file = new File(initPath); |
04 | if (file.isDirectory()) { |
06 | directoryProcess(file); |
07 | } catch (InterruptedException e) { |
08 | System.out.printf( "%s: The search has been interrupted" , |
09 | Thread.currentThread().getName()); |
原文中,提到的方法名称为processDirectory()
。但是,根据下文的程序,属于笔误。故改正。
-
实现directoryProcess()
方法。该方法读取指定目录下的所有文件以及子目录再进行处理。对于每一个目录,该方法进行一个递归调用,来处理参数指定的目录。对于每一个文件,该方法会调用fileProcess()
方法。在处理完所有的目录以及文件后,该方法会检查线程是否被中断,这是抛出一个InterruptedException
异常。代码如下:
05 | * @throws InterruptedException |
07 | private void directoryProcess(File file) throws InterruptedException { |
08 | File[] list = file.listFiles(); |
10 | for ( int i = 0 ; i < list.length; i++) { |
11 | if (list[i].isDirectory()) { |
12 | directoryProcess(list[i]); |
19 | if (Thread.interrupted()) { |
20 | throw new InterruptedException(); |
-
实现fileProcess()
方法,该方法会比较正在处理的文件和需要查找的文件名。如果文件名称相等,则在控制台打印出一条信息。然后,线程检查是否被中断,如果是,则抛出InterruptedException
异常。代码如下:
05 | * @throws InterruptedException |
07 | private void fileProcess(File file) throws InterruptedException { |
08 | if (file.getName().equals(fileName)) { |
09 | System.out.printf( "%s : %s\n" , |
10 | Thread.currentThread().getName(), |
11 | file.getAbsolutePath()); |
14 | if (Thread.interrupted()) { |
15 | throw new InterruptedException(); |
-
现在,来实现示例的主类,并且实现main()
方法。代码如下:
2 | public static void main(String[] args) { |
-
创建并初始化FileSearch
对象,然后创建一个Thread
对象,来执行该任务。然后,启动该线程。代码如下:
1 | FileSearch fileSearch = new FileSearch( "C:\\" , "autoexec.bat" ); |
2 | Thread thread = new Thread(fileSearch); |
-
等待十秒钟,然后中断线程。代码如下:
2 | TimeUnit.SECONDS.sleep( 10 ); |
3 | } catch (InterruptedException e) { |
-
执行该示例,查看结果。
知其所以然
下面是线程执行的结果。从输出中可以看出,当FileSearch
检测到被中断后,如何中止线程执行的。
1 | Thread- 0 : C:\autoexec.bat |
2 | Thread- 0 : The search has been interrupted |
本示例中,我们使用Java的异常来控制线程的中断。当你运行示例时,程序会检测指定目录及其子目录是否包含目标文件。例如,如果输入\b\c\d
,程序将会递归调用三次directoryProcess()
方法。当线程检测到其被中断,则会抛出InterruptedException
异常,无论执行多少次递归调用,程序都会开始执行run()
方法。
永无止境
InterruptedException
异常一般由Java并发API,例如sleep()
方法,抛出。
拿来主义
本文是从 《Java 7 Concurrency Cookbook》 (D瓜哥窃译为 《Java7并发示例集》 )翻译而来,仅作为学习资料使用。没有授权,不得用于任何商业行为。
小有所成
FileSearch类的完整代码
01 | package com.diguage.books.concurrencycookbook.chapter1.recipe4; |
10 | public class FileSearch implements Runnable { |
11 | private String initPath; |
12 | private String fileName; |
17 | * @param initPath 需要进行查找的目录 |
18 | * @param fileName 需要查找的文件名称 |
20 | public FileSearch(String initPath, String fileName) { |
21 | this .initPath = initPath; |
22 | this .fileName = fileName; |
27 | File file = new File(initPath); |
28 | if (file.isDirectory()) { |
30 | directoryProcess(file); |
31 | } catch (InterruptedException e) { |
32 | System.out.printf( "%s: The search has been interrupted" , |
33 | Thread.currentThread().getName()); |
42 | * @throws InterruptedException |
44 | private void directoryProcess(File file) throws InterruptedException { |
45 | File[] list = file.listFiles(); |
47 | for ( int i = 0 ; i < list.length; i++) { |
48 | if (list[i].isDirectory()) { |
49 | directoryProcess(list[i]); |
56 | if (Thread.interrupted()) { |
57 | throw new InterruptedException(); |
65 | * @throws InterruptedException |
67 | private void fileProcess(File file) throws InterruptedException { |
68 | if (file.getName().equals(fileName)) { |
69 | System.out.printf( "%s : %s\n" , |
70 | Thread.currentThread().getName(), |
71 | file.getAbsolutePath()); |
74 | if (Thread.interrupted()) { |
75 | throw new InterruptedException(); |
Main类的完整代码
01 | package com.diguage.books.concurrencycookbook.chapter1.recipe4; |
03 | import java.util.concurrent.TimeUnit; |
11 | public static void main(String[] args) { |
12 | FileSearch fileSearch = new FileSearch( "C:\\" , "autoexec.bat" ); |
13 | Thread thread = new Thread(fileSearch); |
17 | TimeUnit.SECONDS.sleep( 10 ); |
18 | } catch (InterruptedException e) { |