1、实现Callable接口
class MyThread implements Callable{
@Override
public Integer call() throws Exception {
System.out.println("callable");
return 0;
}
}
2、实现runnable接口
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("runnable");
}
}
3、继承Thread
class MyThread3 extends Thread{
@Override
public void run() {
System.out.println("thread");
}
}
runnable与callable的差异
1、实现方法不同
2、是否抛异常
3、有无返回值
启动线程的方式
public static void main(String[] args) throws Exception {
//实现runnable接口
new Thread(new MyThread2()).start();
//继承Thread
new MyThread3().start();
//实现callable
FutureTask<Integer> futureTask = new FutureTask(new MyThread());
new Thread(futureTask,"A").start();
Integer o = futureTask.get();
System.err.println((Integer)o);
}