Threading interrupt control for JAVA concurrent programming

Source: Internet
Author: User

today, take a simple example of the control of the thread interrupt.

Scenario: In a specific directory, look for a specific file, and if found, a 10-second thread is interrupted. If not found, the interruptedexception exception is thrown directly, and the Run () method captures the exception that is handled.

1. Create a class named FileSearch class, and implement the Runnable interface.

public class FileSearch implements runnable{}

2. Declare two private properties, one is the directory of the file that will be found, and the other is the file name. and implement the constructor to initialize the two properties.

File directory
Private String Initpath;
File name
Private String FileName;

Public FileSearch (String Initpath, String fileName) {
This.initpath = Initpath;
This.filename = FileName;
}

3. In the FileSearch class, implement the Run () method. In the Run () method, check the relevant directory and, if any, call the Directoryprocess () method. Otherwise, the interruptedexception exception is thrown and the exception is caught

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 ());
}
}
}

4. Implement the Directoryprocess () method, which takes all the files and subfolders in a folder and processes them. For each directory, it will be called recursively. The Fileprocess () method is called for files in the folder. After processing this file and folder, the thread is checked for interruption, and the interruptedexception exception is thrown.

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 ();
}
}
5. Implement the Fileprocess () method. Will compare the current file's filename and the file name to look for, if the match is to tell the message printed on the console, after the conversation, the thread will check whether it is interrupted, and if so, throw a interruptedexception exception.
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 ();
}
}
6. Write a main class and write a method that contains the main () method.
public class Main {
public static void Main (string[] args) {}

7. Create an object of the FileSearch class, use it as an incoming parameter to create a thread object, and then start the thread execution task.

FileSearch search=new FileSearch ("c:\\", "Log.txt");
Thread thread=new thread (search);
Thread.Start ();

8. Wait 10 seconds and then break the thread.

try{
TimeUnit.SECONDS.sleep (10);
}catch (Interruptedexception e) {
E.printstacktrace ();
}
Thread.Interrupt ();
}

9. Running Results

Threading interrupt control for JAVA concurrent programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.