There are three main ways to create threads in Java:
1. Inherit Thread class to create thread class
(1) Define the subclass of the Thread class and override the run method of the class. The method body of the run method represents the task that the thread wants to complete. Therefore, the run() method is called the execution body.
(2) Create an instance of the Thread subclass, that is, create a thread object.
(3) Call the start() method of the thread object to start the thread.
package ; public class FirstThreadTest extends Thread{ int i = 0; //Rewrite the run method, the method body of the run method is the on-site execution body public void run() { for(;i<100;i++){ (getName()+" "+i); } } public static void main(String[] args) { for(int i = 0;i< 100;i++) { (().getName()+" : "+i); if(i==20) { new FirstThreadTest().start(); new FirstThreadTest().start(); } } } }
In the above code, the() method returns the thread object currently executing. The GetName() method returns the name of the thread that called the method.
2. Create thread classes through the Runnable interface
(1) Define the implementation class of the runnable interface and override the run() method of the interface. The method body of the run() method is also the thread execution body of the thread.
(2) Create an instance of the Runnable implementation class and use this instance as a Thread target to create a Thread object. This Thread object is the real thread object.
(3) Call the start() method of the thread object to start the thread.
The sample code is:
package ; public class RunnableThreadTest implements Runnable { private int i; public void run() { for(i = 0;i <100;i++) { (().getName()+" "+i); } } public static void main(String[] args) { for(int i = 0;i < 100;i++) { (().getName()+" "+i); if(i==20) { RunnableThreadTest rtt = new RunnableThreadTest(); new Thread(rtt,"New Thread 1").start(); new Thread(rtt,"New Thread 2").start(); } } } }
3. Create threads through Callable and Future
(1) Create an implementation class of the Callable interface and implement the call() method. The call() method will be used as the thread execution body and has a return value.
(2) Create an instance of the Callable implementation class and use the FutureTask class to wrap the Callable object. The FutureTask object encapsulates the return value of the call() method of the Callable object.
(3) Create and start a new thread using the FutureTask object as the target of the Thread object.
(4) Call the get() method of the FutureTask object to obtain the return value after the child thread is executed
Example code:
package ; import ; import ; import ; public class CallableThreadTest implements Callable<Integer> { public static void main(String[] args) { CallableThreadTest ctt = new CallableThreadTest(); FutureTask<Integer> ft = new FutureTask<>(ctt); for(int i = 0;i < 100;i++) { (().getName()+"The value of loop variable i"+i); if(i==20) { new Thread(ft,"Thread with return value").start(); } } try { ("Return value of child thread:"+()); } catch (InterruptedException e) { (); } catch (ExecutionException e) { (); } } @Override public Integer call() throws Exception { int i = 0; for(;i<100;i++) { (().getName()+" "+i); } return i; } }
2. Comparison of three ways to create threads
When you use the method of implementing Runnable and Callable interfaces to create multi-threading, the advantages are:
Thread classes only implement the Runnable interface or Callable interface, and can also inherit other classes.
In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU, code and data can be separated to form a clear model, which better reflects the object-oriented idea.
The disadvantages are:
Programming is a little more complicated, if you want to access the current thread, you must use the() method.
The advantages of creating multithreading using inheriting the Thread class are:
It is easy to write. If you need to access the current thread, you don’t need to use the() method, and you can get the current thread directly using this.
The disadvantages are:
The thread class has inherited the Thread class, so it cannot inherit other parent classes.