import java.util.concurrent.Callable;
/**
* @author silence
* Callable创建多线程
* 创件一个类,实现 Callable 《》 这里填返回值类型
* 重写call 方法。
*/
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
for (int i = 0; i < 100; i++) {
System.out.print("callable:");
System.out.print(Thread.currentThread().getName()+"--");
System.out.println(i);
}
return "callable执行完了";
}
}
========================
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class Demo1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable callable1 = new MyCallable();
FutureTask futuretask = new FutureTask(callable1);
//创建多线程
Thread thread = new Thread(futuretask);
//启动多线程
thread.start();
//获取返回值
String o = (String) futuretask.get();
System.out.println(o);
}
}