Future接口
public interface Future<V> {
/**
* 尝试取消执行此任务。
* mayInterruptIfRunning {@code true}如果执行此操作的线程任务应该中断; 否则,允许进行中任务去完成
*/
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
/**
* 阻塞等待结果
*/
V get() throws InterruptedException, ExecutionException;
/**
* 阻塞等待结果,如果超过超时时间则抛出异常
*/
V get(long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;
}
sleep线程样例
@Test
public void FutureTest() throws Exception{
Runnable sleepRun = new Runnable() {
@Override
public void run() {
System.out.println("runnable start");
try {
Thread.sleep(10000);
} catch (InterruptedException