There are three main ways to create threads in Java:
Inheriting Thread Class to Create Thread Class
(1) Define a subclass of the Thread class and override the run method of that class. The body of the run method represents the task that the thread wants to accomplish. So the run() method is called an executor.
(2) Create an instance of Thread subclass, that is, create thread objects.
(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 body of the run method is the field executor.
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 above code returns the thread object currently being executed. The GetName() method returns the name of the thread calling the method.
2. Creating Thread Classes through Runnable Interface
(1) Define the implementation class of the runnable interface and override the run() method of the interface. The run() method body is also the thread executor of the thread.
(2) Create an instance of the Runnable implementation class and use it as the target of Thread to create the Thread object, which is the real thread object.
(3) Call the start() method of the thread object to start the thread.
The 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();
}
}
}
}
3. Creating threads through Callable and Future
(1) Create an implementation class of the Callable interface and implement the call() method, which acts as a thread executor with a return value.
(2) Create an instance of the Callable implementation class and wrap the Callable object with the FutureTask class, which encapsulates the return value of the Callable object's call() method.
(3) Use the FutureTask object as the target of the Thread object to create and start new threads.
(4) Call the get() method of the FutureTask object to get the return value of the subthread after execution
Example 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()+" Cyclic variables i Value"+i);
if(i==20)
{
new Thread(ft,"Threads with return values").start();
}
}
try
{
System.out.println("The return value of the subthread:"+ft.get());
} catch (InterruptedException e)
{
e.printStackTrace();
} catch (ExecutionException e)
{
e.printStackTrace();
}
}
@Override
public Integer call() throws Exception
{
int i = 0;
for(;i<100;i++)
{
System.out.println(Thread.currentThread().getName()+" "+i);
}
return i;
}
}
Comparisons of three ways of creating threads
When multithreading is created by implementing Runnable and Callable interfaces
The advantages are:
Thread classes only implement Runnable or Callable interfaces, and can inherit other classes.
In this way, multiple threads can share the same target object, so it is very suitable for multiple threads to handle the same resource. Thus, CPU, code and data can be separated to form a clear model, which better reflects the idea of object-oriented.
The disadvantages are:
Programming is slightly more complex, and if you want to access the current thread, you must use the Thread.currentThread() method.
When creating multithreads by inheriting the Thread class
The advantages are:
It's easy to write. If you need to access the current thread, you don't need to use the Thread.currentThread() method to get the current thread directly using this.
The disadvantages are:
Thread classes have inherited Thread classes, so they can no longer inherit other parent classes.
Change from: http://blog.csdn.net/longshengguoji/article/details/41126119