If the Java program does not have only one thread of execution, the program will run at the end of all threads. Rather, the program runs the end of all non-daemon threads at the end of the run, or when one of the threads calls the System.exet () method.
Java provides an interrupt mechanism that we can use to end a thread. We create a thread that makes it run for 5 seconds and then forces it to terminate by means of an interrupt mechanism. The program checks whether the number is prime.
Package Com.concurrency;public class Primegenerator extends thread{//inherit from the Thread class and overwrite the Run method. @Overridepublic void Run () {Long number = 1l;//checks whether a digit is prime while (true) {if (IsPrime (number)) {System.out.println (number) ;//is a prime number on print}if (isinterrupted ()) {//per check to see if the thread is interrupted System.out.println ("has been interrupted!"); return;} number++;}} Private Boolean IsPrime (long number) {//function to determine prime number if (number < 2) return true;for (Long i = 2; i < number; i++) {if (number % i) = = 0) {return false;}} return true;} public static void Main (string[] args) {Thread task = new Primegenerator ();//Create Object Task.start ();// Calling start runs the run function. It will lead to a thread to the side to execute. The value function will continue to execute downward, and the two do not interfere with Try{thread.sleep (2000);//Let the thread hibernate for 2 seconds} catch (Interruptedexception e) {e.printstacktrace ();} Task.interrupt ();}}
The above is how to break the thread in execution, which can be controlled in the Threads object. There are better mechanisms to control thread interrupts, and Java provides interruptedexception exceptions to support. This exception is thrown when a thread break is checked and then caught and handled in run.
The purpose of the following method is to find out whether a file exists in the specified path. You can iterate through the Find folder.
Package Com.concurrency;import Java.io.file;import Java.util.concurrent.timeunit;public class FileSearch implements Runnable{private string initpath;//Initial file path to retrieve private string filename;//the filename to retrieve public FileSearch (String intistring, String fileName) {This.initpath = Intistring;this.filename = FileName;} @Overridepublic void Run () {//Implements the interface also is to implement the Run method file File = new file (Initpath); if (File.isdirectory ()) {try{ Directoryprocess (file),//through the processing path of the iteration} catch (Interruptedexception e) {//Just throw an exception here to capture 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 ()) {//If the thread is interrupted, throw an exception here, catch an exception in run throw new Interruptedexception ();}} private void fileprocess (file file) throws Interruptedexception{if (File.getname (). Equals (Filename) {//check found file System.out.printf ("%s:%s \ n", Thread.CurrentThread (). GetName (), File.getabsolutepath ());} if (thread.interrupted ()) {throw new Interruptedexception ();}} public static void Main (string[] args) {FileSearch searcherfilesearch = new FileSearch ("c:\\", "Autoexec.bat"); Thread thread = new Thread (searcherfilesearch); Thread.Start (); Try{timeunit.seconds.sleep (ten);} catch ( Interruptedexception e) {e.printstacktrace ();} Thread.Interrupt ();//In this case, because the thread in a few complex methods, and has a recursive method//call. To catch an exception by means of an exception}}
Java Multithreading (2)--thread interrupt and interrupt control