一种是extends Thread,好处是简单,缺点是占用继承资源
一种是implements Runnable,好处是Java可以多实现,使用方式稍微复杂
还有一种是不太常用的,好处是可以有返回值:implements Callable<V>
package cn.bl.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class MyCall <V> implements Callable<V>{
@Override
public V call() throws Exception {
System.out.println(11);
return null;
}
//测试:
public static void main(String[] args) {
Callable<Object> callable = new MyCall<>();
FutureTask<Object> task = new FutureTask<>(callable);
Thread thread = new Thread(task);
thread.start();
}
}