0
点赞
收藏
分享

微信扫一扫

future相关

Future是Java 5添加的类,用来描述一个异步计算的结果。你可以使用isDone方法检查计算是否完成,或者使用get阻塞住调用线程,直到计算完成返回结果,你也可以使用cancel方法停止任务的执行。
虽然Future以及相关使用方法提供了异步执行任务的能力,但是对于结果的获取却是很不方便,只能通过阻塞或者轮询的方式得到任务的结果。阻塞的方式显然和我们的异步编程的初衷相违背,轮询的方式又会耗费无谓的CPU资源,而且也不能及时地得到计算结果,为什么不能用观察者设计模式当计算结果完成及时通知监听者呢?

1.jdk的Future

public class TestJdkFuture {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService threadPool = Executors.newFixedThreadPool(2);
// future - jdk
// Runnable
// Future future = threadPool.submit(() -> {
// System.out.println(1);
// });

// Callable
Future future1 = threadPool.submit(new Callable() {
@Override
public Integer call() throws Exception {
Thread.sleep(1000);
System.out.println(2);
return 1;
}
});
System.out.println(3);
// 3 2 1
System.out.println(future1.get());
}
}

2.Netty的Future

public class TestNettyFuture {
public static void main(String[] args) throws ExecutionException, InterruptedException {
EventLoop eventLoop = new DefaultEventLoopGroup().next();
// Runnable
// eventLoop.submit(() -> {
// System.out.println("1");
// });

// Callable
Future future = eventLoop.submit(new Callable() {
@Override
public Integer call() throws Exception {
System.out.println(2);
Thread.sleep(1000);
return 1;
}
});

System.out.println(3);
System.out.println(future.get());
}
}

3.FutureTask

public class TestFutureTask {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask futureTask = new FutureTask<>(() -> {
Thread.sleep(1000);
System.out.println(2);
return 1;
});

System.out.println(3);
new Thread( futureTask, "t1").start();
// 3 2 1
System.out.println(futureTask.get());
}
}

4.Promise的Future

public class TestPromise {
public static void main(String[] args) throws Exception, InterruptedException {
EventLoop eventLoop = new NioEventLoopGroup().next();
DefaultPromise promise = new DefaultPromise<>(eventLoop);

new Thread(()->{
System.out.println(2);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
promise.setSuccess(1);
}, "t1").start();

System.out.println(3);
// 3 2 1
System.out.println(promise.get());
}
}

5.CompletableFuture

future相关_ide
创建一个异步操作

public static CompletableFuture runAsync(Runnable runnable)
public static CompletableFuture runAsync(Runnable runnable, Executor executor)
public static CompletableFuture supplyAsync(Supplier supplier)
public static CompletableFuture supplyAsync(Supplier supplier, Executor executor)

public class TestCompletableFuture02 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 20, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>(50), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

// CompletionStage
CompletableFuture future1 = CompletableFuture.supplyAsync(() -> {
try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
return 1;
}).thenApply(r -> {
return r + 2;
}).whenComplete((v, e) -> {
if (e == null) {
System.out.println("result : " + v);
}
}).exceptionally(e -> {
e.printStackTrace();
return null;
});
System.out.println("main");
try { TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}
threadPool.shutdown();
}

private static void m1() throws InterruptedException, ExecutionException {
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 20, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>(50), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

CompletableFuture future1 = CompletableFuture.supplyAsync(() -> {
try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
return 1;
}, threadPool);
System.out.println(future1.get());
// 1 main:阻塞

System.out.println("main");

threadPool.shutdown();
}
}

future相关_ide_02


举报

相关推荐

0 条评论