1 thread management
In this section, we will learn how to create and run a thread. In Java, we can create a thread in two ways:
1. Extend the Thread class and overwrite the run () method.
2. Construct a class that implements the Runnable interface, define its own run () method, and then use the Instance Object of the class as the parameter of the Thread constructor to create the Thread object.
Create a thread object. It does not create a new execution thread. That is to say, call the run () method of a class that implements the Runnable interface, and it will not create a new execution thread. A new thread is created only when the start () method is called.
The Thread class saves some attributes that can help us identify a Thread.
1. ID: Unique Identifier of each thread.
2. Name: Name of the storage thread.
3. Priority: the Priority attribute of the thread object. The value range is 0 to 10. Unless there are special requirements, it is generally not recommended to modify the Priority attribute of the thread.
4. Status: This attribute indicates the current state of the thread. in Java, there are six States: new and runnable.
Blocked, time waiting, and terminated.
Create a simple thread program.
public class Worker implements Runnable{ private intnumber; /** * Constructor of the class * @param number : The number */ public Worker(intnumber) { this.number=number; } /** * Method that do the calculations */ @Override public voidrun() { for (int i=1; i<=10; i++){ System.out.printf("%s: %d * %d = %d\n",Thread.currentThread().getName(),number,i,i*number); } } /** * @param args */ public staticvoidmain(String[] args) { //create 10 thread, chart one long t1 = System.currentTimeMillis(); for(int i = 0 ; i < 10; i++){ Worker worker = new Worker(i); Thread thread = new Thread(worker); thread.start(); } System.out.println("-----------------------"+(System.currentTimeMillis() - t1)); } }
Scan the files in the specified file directory by viewing another file search function. The complete code is as follows:
import java.io.File;import java.util.concurrent.TimeUnit; public class FileSearchRunnable implements Runnable { /** * Initial path for the search */ private StringinitPath; /** * Name of the file we are searching for */ private StringfileName; /** * Constructor of the class * * @param initPath * : Initial path for the search * @param fileName * : Name of the file we are searching for */ public FileSearchRunnable(String initPath, String fileName) { this.initPath = initPath; this.fileName = fileName; } /** * Main method of the class */ @Override public voidrun() { 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()); cleanResources(); } } } /** * Method for cleaning the resources. In thiscase, is empty */ private voidcleanResources() { } /** * Method that process a directory * * @param file * : Directory to process * @throws InterruptedException * : If the thread is interrupted */ private voiddirectoryProcess(File file) throws InterruptedException { // Get the content of the directory File list[] = file.listFiles(); if (list !=null) { for (int i = 0; i < list.length; i++) { if (list[i].isDirectory()) { // If is a directory, process it directoryProcess(list[i]); } else { // If is a file, process it fileProcess(list[i]); } } } // Check the interruption if (Thread.interrupted()) { throw new InterruptedException(); } } /** * Method that process a File * * @param file * : File to process * @throws InterruptedException * : If the thread is interrupted */ private voidfileProcess(File file) throws InterruptedException { System.out.println("current file : "+file.getAbsolutePath()); // Check the name if (file.getName().equals(fileName)) { System.out.printf("%s : %s\n",Thread.currentThread().getName(),file.getAbsolutePath()); System.exit(0); } // Check the interruption if (Thread.interrupted()) { throw new InterruptedException(); } } /** * @param args */ public staticvoidmain(String[] args) { FileSearchRunnable searcher=new FileSearchRunnable("C:\\","USERPRIKEY.HS"); Thread thread=new Thread(searcher); // Starts the Thread thread.start(); // Wait for ten seconds try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }