java创建线程的方式
java创建线程的方式有哪些呢?我们一般想到的方式是两种:继承Thread类、实现Runnable接口。但是这两种创建线程的方式并没有本质的区别。从本质上来讲,创建线程的方式只有一种。
首先我们先来看看怎么通过继承Thread类怎么创建线程
// 通过继承Thread类创建线程
public class ThreadStyle extends Thread {
@Override
public void run() {
System.out.println("extends thread create thead");
}
public static void main(String[] args) {
new ThreadStyle().start();
}
}
ThreadStyle类重写了Thread类的方法,然后通过创建一个ThreadStyle对象的方式来创建线程。
我们接下来看下通过实现Runnable接口的方式来创建线程
// 通过实现Runnable接口的方式创建对象
public class RunnableStyle implements Runnable{
@Override
public void run() {
System.out.println("implement runnable create thead");
}
public static void main(String[] args) {
new Thread(new RunnableStyle()).start();
}
}
RunnableStyle 实现了了Runnable接口的run方法,创建一个参数为RunnableStyle的Thread对象。
以上我们分别通过继承Thread类和实现Runnable接口这两种方式创建了线程。但是本质上它们都是通过创建Thread对象的方式创建对象。它们的区别是ThreadStyle重写了Thread类的run方法,RunnableStyle 没有。我们接下来看下Thread的run方法。
/**
* If this thread was constructed using a separate
* <code>Runnable</code> run object, then that
* <code>Runnable</code> object's <code>run</code> method is called;
* otherwise, this method does nothing and returns.
* <p>
* Subclasses of <code>Thread</code> should override this method.
*
* @see #start()
* @see #stop()
* @see #Thread(ThreadGroup, Runnable, String)
*/
@Override
public void run() {
if (target != null) {
target.run();
}
}
首先我们看下注释
1:如果thread的构造方法中传入了一个Runnable对象,那么这个Runnable对象的run方法就会被调用。否则什么也不做。这和我们第二种实现方式一样。我们在创建Thread对象的时候传入了RunnableStyle 对象。
2:子类必须重写这个方法。我们第一中方式就是这样做的,ThreadStyle 类重写了Thread类的run方法。那么Thread的run方法就不会被调用,会直接调用子类的run方法。
接下来通过代码来看下同时用这两种方式创建线程会发生什么。
// 同时用实现runnable接口和继承thread方式创建线程
public class TwoMethodCreateThread {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("runnable 方法");
}
}){
@Override
public void run(){
System.out.println("thread 方法");
}
}.start();
}
}
这段代码运行的结果是什么呢?如果理解了Thread的run方法,应该不难猜到。
如果去创建一个线程,我们应该优先通过实现Runnable接口的方式去实现呢。理由有以下三点。
1:从架构解耦的角度考虑,实现Runnable接口的方式与Thread类的耦合度更低。
2:从创建线程的消耗角度考虑,如果要创建大量线程,比如线程池,Runnable的性能消耗会更低。
3:继承只能单继承,实现可以多实现。
我们还有很多方式可以创建线程,比如Callable、线程池、Timer等方式。但这些方式本质上还是通过实现Runnable接口方式来创建线程的,只不过进行了层层包装。下面来分析下通过Callable方式是如何创建线程。
// 通过callable方式创建线程
public class CallableStyle implements Callable {
@Override
public Object call() throws Exception {
return "hello";
}
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
Future future = executor.submit(new CallableStyle());
try {
System.out.println(future.get());
} catch (Exception e) {
}
}
}
1: 创建一个线程池对象executor。
2:调用executor的submit方法,传入参数new CallableStyle(),CallableStyle实现了Callable接口的call方法。返回一个future对象。
接下来看下executor的submit方法。
/**
* @throws RejectedExecutionException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public <T> Future<T> submit(Callable<T> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<T> ftask = newTaskFor(task);
execute(ftask);
return ftask;
}
newTaskFor(task);方法将Callable对象转换成了RunnableFuture对象。这里能看到Callable和Runnable的联系了。我们接下来看下RunnableFuture对象。
/**
* A {@link Future} that is {@link Runnable}. Successful execution of
* the {@code run} method causes completion of the {@code Future}
* and allows access to its results.
* @see FutureTask
* @see Executor
* @since 1.6
* @author Doug Lea
* @param <V> The result type returned by this Future's {@code get} method
*/
public interface RunnableFuture<V> extends Runnable, Future<V> {
/**
* Sets this Future to the result of its computation
* unless it has been cancelled.
*/
void run();
}
RunnableFuture,同时继承了Runnable和Future的接口。本质上来说,Callable方式也是通过继承Runnable接口来创建线程的。
通过以上说明,可以看到。创建线程的外在表现形式有很多种,但本质上都是通过创建Thread对象的方式来创建线程。其它方式不过是对这种方式的包装。