820. 线程实现的⽅式有⼏种(四种)?

  1. 继承Thread类,重写run⽅法
  2. 实现Runnable接⼝,重写run⽅法,实现Runnable接⼝的实现类的实例对象作为Thread构造函数的target
  3. 实现Callable接⼝通过FutureTask包装器来创建Thread线程
  4. 通过线程池创建线程
    public class ThreadDemo03 {
    2 public static void main(String[] args) {
    3 Callable<Object> oneCallable = new Tickets<Object>();
    4 FutureTask<Object> oneTask = new FutureTask<Object>(oneCallable);
    5 Thread t = new Thread(oneTask);
    6 System.out.println(Thread.currentThread().getName());
    7 t.start();
    8 }
    9 }
    10
    11 class Tickets<Object> implements Callable<Object>{
    12 //重写call⽅法
    13 @Override
    14 public Object call() throws Exception {
    15 // TODO Auto-generated method stub
    16 System.out.println(Thread.currentThread().getName()+"-->我是通过实现Callable接⼝通过FutureTask包装器来实现的线程"
    17 return null;
    18 } 
    19 }