Detailed and contrast three ways to create threads

.Java three ways to create a thread

Java threads are created there are three main ways:

1. Thread class inheritance

2. implement Runnable

3. Use Callable and Future

1. Create a class inherits Thead thread

(1) Thread class inheritance and override the run method

(2) create a thread object

(3) call the thread object's start () method to start a thread

public class CreateThreadTest {
public static void main(String[] args) {
new ThreadTest().start();
new ThreadTest().start();
}
}
class ThreadTest extends Thread{
private int i = 0;
@Override
public void run() {
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " is running: " + i);
}
}
}

2. Implement Runnable interface to create threads

(1) the definition of a class that implements Runnable interface, the interface and override run () method

(2) create an object Runnable implementation class, create a Thread object as a target parameter, this is the real Thread object thread object

(3) the calling thread object's start () method to start a thread

public class CreateThreadTest {
public static void main(String[] args) {
RunnableTest runnableTest = new RunnableTest();
new Thread(runnableTest, "线程1").start();
new Thread(runnableTest, "线程2").start();
}
}
class RunnableTest implements Runnable{
private int i = 0;
@Override
public void run() {
for (; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + " is running: " + i);
}
}
}

3. Use Callable and Future create a thread

And Runnable interface is not the same, Callable interface provides a call () method as a thread of execution, call () method () method function to be more powerful than run: call () method can return a value, you can declare an exception is thrown.

public interface Callable<V> {
V call() throws Exception;
}

Providing Future Java5 Callable interface to receive the return value of the interface call () method. Java5 Callable interface is a new interface, not the sub-interface Runnable interface, so Callable objects can not be directly used as target Thread object. To address this issue, the introduction of RunnableFuture interfaces, RunnableFuture interface is a sub-interface Runnable interface and Future interfaces can be used as target Thread object. Meanwhile, Java5 provides a class that implements interface RunnableFuture: FutureTask, FutureTask Thread object can be used as target.

Detailed and contrast three ways to create threads

 

After introducing the concepts, and the step of using Callable Future threads created as follows: (1) the definition of a class that implements Callable interface and override call () method, the call () method as a thread of execution, and returns a value

(2) create an instance of the implementation class Callable used to wrap Callable object class FutureTask

(3) use FutureTask objects created as a target Thread object and start the thread

(4) calls FutureTask object's get () method to obtain the return value after the end of the sub-thread execution

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CreateThreadTest {
public static void main(String[] args) {
CallableTest callableTest = new CallableTest();
FutureTask<Integer> futureTask = new FutureTask<>(callableTest);
new Thread(futureTask).start();
try {
System.out.println("子线程的返回值: " + futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class CallableTest implements Callable{
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 1; i < 101; i++) {
sum += i;
}
System.out.println(Thread.currentThread().getName() + " is running: " + sum);
return sum;
}
}

II. Comparison of three ways to create a thread

1. To achieve Runnable / Callable interface inheritance Thread class advantages in comparison

(1) for multiple threads share resources

(2) avoid java limitations in single inheritance

Robustness, code and data (3) increasing process independent

(4) thread pool can only put Runable or Callable interface class, not directly into the class inheritance Thread

The difference 2.Callable and Runnable

(1) Callable is rewritten call () method, a method is overridden Runnable run () method

(2) call () may be performed after the method has a return value, run () method does not return value

(3) call () method can throw an exception, run () method can not

(4) Run the task can get a Future Callable object that represents the result of an asynchronous computation. You can understand the implementation of the mandate, to cancel the task by Future object to perform, but also get the results

Thank you for reading this article

I am a small frame, we

See Mid-Autumn Festival, I wish you all a happy Mid-Autumn Festival!

Guess you like

Origin www.cnblogs.com/sevencutekk/p/11515225.html