java 如何创建一个有返回值的线程

//使用Future 和 Callable  


public class MyFuture {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
Future<String> future = singleThreadExecutor.submit(new MyTask());
MyTask task = new MyTask();
System.out.println(future.get());
}
static class MyTask implements Callable<String>{


@Override
public String call() throws Exception {
System.out.println("hehe");
return "返回值";
}

}

}

猜你喜欢

转载自blog.csdn.net/qq_27986007/article/details/78499377