创建线程的方式三:实现Callable接口(未结合泛型)
package com.callable.bean;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CallableThird {
public static void main(String[] args) {
Test t = new Test();
FutureTask f = new FutureTask(t);
new Thread(f).start();
Object sum = null;
try {
sum = f.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
System.out.println(sum);
}
}
class Test implements Callable{
public Object call() throws Exception{
int sum = 0;
for (int i = 1; i <=100 ; i++) {
if(i%2==0){
System.out.println(i);
}
sum += i;
}
return sum;
}
}