There are three ways to create a Thread: java
Inherit Thread class
The Thread object will start to compete for resources. The tasks to be executed by this Thread should be placed in the method, and this method cannot be written casually. It must override the run method in the Thread class, and the task / logic of the Thread should be written in the run method
Child thread creation
public class CT01 extends Thread { @Override public void run() { //Child thread output System.out.println("Thread-----this is thread"); } }
Main thread call
If it is not possible to call the run method to execute the tasks in the thread, calling run() directly will be regarded as an ordinary method, and the thread will not rob resources
public class Test { public static void main(String[] args) { //Output of the first section of the main thread: System.out.println("main1-----"); //To create other threads, you should compete with the main thread for resources. The specific thread object is sub thread CT01 ct = new CT01(); ct.run(); //Output of the second section of the main thread: System.out.println("main2-----"); } } main1----- Thread-----this is thread main2-----
If you want ct sub threads to really work, for example, to start a Thread, you need to use the start() method, which is the method in the Thread class
public class Test { public static void main(String[] args) { //Output of the first section of the main thread: System.out.println("main1-----"); //To create other threads, you should compete with the main thread for resources. The specific thread object is sub thread CT01 ct = new CT01(); // ct.start(); //Output of the second section of the main thread: System.out.println("main2-----"); } } main1----- main2----- Thread-----this is thread
The thread status of java programs is as follows. Even if we only use one thread, there will actually be exception handling threads and garbage cleaning threads, and the exception thread will affect the execution of the main thread
Implement Runnable interface
Child thread call
public class CT02 implements Runnable{ @Override public void run() { System.out.println("Thread-----This is Runnable"); } }
Main thread call
You can't call start() directly here, because there is no start() method in the created CT02 class, which is in the thread class. It needs to be converted to thread, and the thread class provides a constructor of public Thread(Runnable target), which can be just created based on the Runnable implementation class. start() goes through layers of nesting and eventually goes to the run() method in the implementation class
public class Test { public static void main(String[] args) { //Output of the first section of the main thread: System.out.println("main1-----"); CT02 ct02 = new CT02(); Thread ct = new Thread(ct02);//Thread(Runnable target) constructor ct.start(); //Output of the second section of the main thread: System.out.println("main2-----"); } } main1----- main2----- Thread-----This is Runnable
Implement Callable interface
Comparing the first and second methods of creating threads, it is found that whether you inherit the Thread class or implement the Runnable interface, you need a run() method:
@Override public void run() { }
However, this method has no return value and cannot throw an exception. Therefore, after JDK 1.5, there is a third thread creation method to implement the Callable interface.
- Implement the Callable interface with generic options
- If there is a generic type, the return value of call() is the type corresponding to the generic type
- Without generics, the return value of the call() method defaults to the Object type
- The call() method has a return value and can throw exceptions, but thread creation is troublesome
import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; public class CallTest implements Callable<String> { @Override public String call() throws Exception { return "Thread-----This is Callable"; } } class Test{ //This is the main method, the entry to the program public static void main(String[] args) throws ExecutionException, InterruptedException { //When defining a thread object, it needs to go through three layers of nesting CallTest ct = new CallTest(); FutureTask ft = new FutureTask(ct); Thread t = new Thread(ft); t.start(); //Get the return value obtained by the thread: Object obj = ft.get(); System.out.println(obj); } }