There are three main ways of creating threads in Java:
First, inherit the thread class to create a threading class
(1) Defines a 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 is about to complete. Therefore, the run () method is called the running body.
(2) Create an instance of the thread subclass, that is, the threading object is created.
(3) Call the Start () method of the thread object to start the thread.
Package Com.thread;public class Firstthreadtest extends thread{int i = 0;//rewrite the Run method. The method body of the Run method is the field run body public void run () {for (; i<100;i++) {System.out.println (GetName () + " " +i);}} public static void Main (string[] args) {for (int i = 0;i< 100;i++) {System.out.println (Thread.CurrentThread (). GetName ( ) + " :" +i); if (i==20) {new Firstthreadtest (). Start (); New Firstthreadtest (). Start ();}}}
The Thread.CurrentThread () method in the preceding code returns the currently running thread object. The GetName () method returns the name of the thread that called the method.
Second, create the thread class through the Runnable interface
(1) Define the implementation class for the Runnable interface and override the run () method of the interface. The method body of the run () method is the thread running body of the thread.
(2) Create an instance of the Runnable implementation class, and use this instance as the target of the thread to create the thread object. This thread object is the real thread object.
(3) Call the Start () method of the thread object to start the thread.
The Demo sample code is:
Package Com.thread;public class Runnablethreadtest implements Runnable{private int. I;public void Run () {for (i = 0;i <100 ; i++) {System.out.println (Thread.CurrentThread (). GetName () + "" +i);}} public static void Main (string[] args) {for (int i = 0;i < 100;i++) {System.out.println (Thread.CurrentThread (). GetName ( ) + "" +i), if (i==20) {runnablethreadtest RTT = new Runnablethreadtest (), new Thread (RTT, "new Thread 1"). Start (); New Thread (RTT, " New Thread 2 "). Start ();}}}
Iii. creating threads through callable and future
(1) Create an implementation class for the callable interface and implement the call () method, which will act as the thread body. And there is a return value.
(2) Create an instance of the callable implementation class. Use the Futuretask class to wrap the callable object, which encapsulates the return value of the call () method of the Callable object.
(3) Use the Futuretask object as the target of the thread object to create and start a new thread.
(4) Call the Get () method of the Futuretask object to get the return value after the child thread finishes running
Instance code:
Package Com.thread;import Java.util.concurrent.callable;import Java.util.concurrent.executionexception;import Java.util.concurrent.futuretask;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++) {System.out.println ( Thread.CurrentThread (). GetName () + "The value of the loop variable i" +i), if (i==20) {New Thread (ft, "thread with return value"). Start ();}} Try{system.out.println ("The return value of the child thread:" +ft.get ()),} catch (Interruptedexception e) {e.printstacktrace ();} catch ( Executionexception e) {e.printstacktrace ();}} @Overridepublic Integer Call () throws Exception{int i = 0;for (; i<100;i++) {System.out.println (Thread.CurrentThread ( ). GetName () + "" +i); return i;}}
Two, three ways to create a thread control
When you implement runnable, callable interface, transcend multi-Threading. The advantages are:
The thread class simply implements the Runnable interface or the callable interface. Can also inherit other classes.
In such a way, multiple threads can share the same target object. So it is suitable for multiple same threads to handle the same resource, so that the CPU, code and data can be separated to form a clear model, which embodies the idea of object-oriented.
Disadvantages are:
Programming is slightly more complex. Suppose you want to access the current thread, you must use the Thread.CurrentThread () method.
The advantage of creating multiple threads with the inherited thread class is:
Writing is simple. If you need to access the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.
Disadvantages are:
Thread classes have inherited the thread class. Therefore no other parent class can be inherited.
Three ways to create threads in Java and their controls