通过上面代码可以看出Thread类自身实现了Runnable接口,并在创建Thread类时,通过有参构造写入,将Thread类中的target属性进行赋值,并在调用线程的时候执行你传入的Thread类的run方法。
public class CallableTest implements Callable<Integer> {
private int num = 0;
@Override
public Integer call() throws Exception {
for (int i = 0; i < 10; i++) {
num++;
}
return num;
}
public static void main(String[] args) throws Exception {
Callable<Integer> callable = new CallableTest();
FutureTask<Integer> futureTask = new FutureTask<>(callable);
Thread t = new Thread(futureTask);
t.start();
System.out.println(futureTask.get());
}
}