Creating thread classes by inheriting the thread class implementing the Runnable interface creating a thread class using callable and future creating threads comparison of three ways to create multithreading with the implementation of the Runnablecallable interface Create multithreading pros and cons with inheritance thread class
Java uses the thread class to represent threads, and all thread objects must be instances of the thread class or its subclasses. Each next time you hum the role is to complete a certain task, is actually to execute a program flow (a sequence of code execution). Java uses the thread execution body to represent this stream of programs
There are three ways to create Java threads
creating a thread class by inheriting the thread class
The steps below define the subclass of the thread class and override the run () method of the class, which represents the task the thread needs to complete. So the run () method is called the thread execution body. Creates an instance of the thread subclass, that is, the object that created it. Invokes the start () method of the thread object to start the thread.
/**
* Create thread class through inheriting thread class
* @author Administrator
*/public
class Firstthread extends thread{
private int i;
public void Run () {for
(i=0;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 Firstthread (). Start ();
New Firstthread (). Start ();}}}
implementing the Runnable interface to create a thread class
The steps are as follows: Define the implementation class for the Runnable interface and override the run () method of the interface, which is also the thread execution body of the thread. Creates an instance of the Runnable implementation class and creates the thread object with this instance as target for thread, which is the true thread object. Invokes the start () method of the thread object to start the thread.
/**
* Create a thread class by implementing the Runnable interface
* @author Administrator */Public
class Secondthread implements runnable{
private int i;
public void Run ()
{
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) {
secondthread st=new secondthread ();
New Thread (St, "1"). Start ();
New Thread (St, "2"). Start ();}}
}
creating threads using callable and future
The
Steps are as follows: Creates an instance of the callable implementation class. and implement the Call () method, which will act as the thread executor, and the call () method has a return value that creates an instance of the callable implementation class. Starting with Java8 , you can create an callable object directly using a lambda expression. Use the Futuretask class to wrap the callable object, which encapsulates the return value of the call () method of the Call object. Creates and starts a new thread using the Futuretask object as target of the thread object. Call the Get () method of the Futuretask object to obtain the return value after the child thread execution ends.
Import java.util.concurrent.Callable;
Import Java.util.concurrent.FutureTask;
/** * Create threads using callable and future * @author Administrator */public class Thirdthead implements callable<integer>{
/** * @param args */public static void main (string[] args) {thirdthead rt=new thirdthead ();
futuretask<integer> task= New futuretask<integer> (RT);
for (int i=0;i<100;i++) {System.out.println (Thread.CurrentThread (). GetName () + "loop variable i value:" +i);
if (i==20) {New Thread (task, "Thread with return value"). Start ();
} try{System.out.println ("The return value of the child thread:" +task.get ());
}catch (Exception ex) {ex.printstacktrace ();
@Override public Integer Call () throws Exception {int i=0;
for (i=0;i<100;i++) {System.out.println (Thread.CurrentThread (). GetName () + "loop variable i value:" +i);
return i;
}
}
three ways to create a comparison
Multithreading can be achieved by inheriting the thread class or implementing runnable, callable interfaces. However, the way to implement runnable and implement callable is basically the same, except that callable has a return value and can throw an exception. Therefore, the runnable and callable are grouped into one class. The difference between this implementation and the inheritance thread approach is as follows: The use of implementing runnable, callable interface to create multithreading advantages and disadvantages: The display is only implemented runnable and callable interface, but also to implement other inherited classes. In this case, multithreading can share the same target object, very suitable for multiple identical threads to handle the same resource, so that the CPU, code and data can be better separated, forming a clear model, a good embodiment of object-oriented thinking. Disadvantage: Programming is more complex, and you must use the Thread.CurrentThread () method if you want to access the current thread. using the inheritance thread class to create multithreading pros and Cons: advantages: Simple to write, if you need to access the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly. Disadvantage: Because a thread has inherited the thread class, no other parent class can be inherited.
Comprehensive analysis: It is recommended to implement Runnable interface, callable interface way to achieve multithreading.