Java provides three ways to create threads:
- By inheriting the Thread class itself;
- By implementing the Runnable interface;
- Create threads from callable and future.
Method One: By inheriting the thread class
① defines a subclass of the thread class and overrides the Run () method of the class, which represents the task that the thread will complete. So the run () method is called the actuator.
② creates an instance of the thread subclass, which is the creation of a threading object.
③ invokes the start () method of the thread object to start the thread.
Example:
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 execution 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 ();//Create an instance object and call the Start method to start the thread
New Firstthreadtest (). Start ();
}
}
}
}
Method Two: By implementing the Runnable interface
① defines the implementation class for the Runnable interface and overrides the run () method of the interface, which is also the thread-executing body of the thread of the Run () method.
② creates an instance of the Runnable implementation class and creates a thread object with this instance as the target of the thread, which is the true thread object .
③ invokes the start () method of the thread object to start the thread.
Example:
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 ();//Create Instance Object
New Thread (RTT, "1"). Start ();//The object that implements the class as a parameter to the thread class, creates a thread object, and starts the thread
New Thread (RTT, "2"). Start ();
}
}
}
}
Method Three: Create threads from callable and future
① creates an implementation class for the callable interface and implements the call () method, which acts as the thread execution body and has a return value .
② creates an instance of the callable implementation class, using the Futuretask class to wrap the callable object, which encapsulates the return value of the call () method of the Futuretask object .
③ uses the Futuretask object as the target of the thread object to create and start a new thread.
④ calls the Get () method of the Futuretask object to get the return value after the child thread execution ends .
Example:
Package com.thread;
Import java.util.concurrent.Callable;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.FutureTask;
public class Callablethreadtest implements callable<integer>{//implementation interface
public static void Main (string[] args) {
Callablethreadtest CTT = new Callablethreadtest ();
futuretask<integer> ft = new futuretask<> (CTT);//Use Futuretask to wrap Callablethreadtest objects
for (int i = 0;i < 100;i + +) {
System.out.println (Thread.CurrentThread (). GetName () + "The value of the cyclic variable i" +i);
if (i = = 20) {
New Thread (FT, "thread with return value"). Start ();//Use the Futuretask object as the target of the thread construction method and start the thread
}
}
try{
System.out.println ("The return value of the child thread:" +ft.get ());//Use the Get method of Futuretask to get the return value of the child thread call () method execution
} catch (Interruptedexception e) {
E.printstacktrace ();
} catch (Executionexception e) {
E.printstacktrace ();
}
}
@Override
Public Integer Call () throws exception{//override the call () method of the callable interface
int i = 0;
for (; i < 100;i + +) {
System.out.println (Thread.CurrentThread (). GetName () + "" +i);
}
return i;
}
}
"Summary" Comparison of three ways to create threads
- When using the runnable, callable interface to transcend multiple threads:
- The advantage is that the thread class simply implements the Runnable interface or callable interface and can 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 is a good embodiment of object-oriented thinking.
- Disadvantages are:
- Programming is slightly more complex, and you must use the Thread.CurrentThread () method if you want to access the current thread.
- The advantage of creating multiple threads with the inherited thread class is:
- Writing is simple and if you need access to the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.
- Disadvantages are:
- The thread class has inherited the thread class, so it is no longer possible to inherit other parent classes.
Essay ① about how Java threads---create threads