标签:com demo future 返回 引入 submit ace 阻塞 except
public class RunnableDemo implements Runnable { public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in runnable demo"); } }
public static void main(String[] args) throws Exception { Thread runnableThread = new Thread(new RunnableDemo()); runnableThread.start(); System.out.println("in main"); }
输出结果
in main
in runnable demo
可以看到线程的运行没有阻塞当前线程
public static void main(String[] args) throws Exception { Thread runnableThread = new Thread(new RunnableDemo()); runnableThread.start(); runnableThread.join(); System.out.println("in main"); }
输出结果
in runnable demo
in main
Join会阻塞当前线程,一直等待自定义线程才返回。
在Runnable的例子中,Runnable接口有一个很大的缺陷就是run方法没有返回值定义,主线程无法获取到线程执行的结果。这个时候就需要Callable接口。
public class CallableDemo implements Callable<CallableDto> { public CallableDto call() throws Exception { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in callable demo"); return new CallableDto(1); } } class CallableDto { private int id; public CallableDto(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
public static void main(String[] args) throws Exception { ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); Future<CallableDto> future = executor.submit(new CallableDemo()); System.out.println("in main"); }
输出结果,如下所示,新启动的线程没有阻塞当前线程
in main
in callable demo
public static void main(String[] args) throws Exception { ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); Future<CallableDto> future = executor.submit(new CallableDemo()); CallableDto callableDto = future.get(); System.out.println("in main"); System.out.println("id from callable is " + callableDto.getId()); }
get方法首先会阻塞主线程,等待当前线程执行结束才返回,且返回线程的执行结果。
CompletableFuture是jdk1.8引入的api,做了进一步的封装,用户线程无需实现Callable接口也能启动,且能够返回用户线程的执行结果
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
public class CompletableFutureDemo { public CompletableFutureDemoDto action() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("in CompletableFutureDemo "); return new CompletableFutureDemoDto(1); } } class CompletableFutureDemoDto { private int id; public CompletableFutureDemoDto(int id) { this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
public static void main(String[] args) throws Exception { ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); CompletableFuture<CompletableFutureDemoDto> future = CompletableFuture.supplyAsync(() -> { return new CompletableFutureDemo().action(); }, executor); System.out.println("in main"); }
执行结果,可以看到,主线程没有被阻塞
in main
in CompletableFutureDemo
public static void main(String[] args) throws Exception { ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); CompletableFuture<CompletableFutureDemoDto> future = CompletableFuture.supplyAsync(() -> { return new CompletableFutureDemo().action(); }, executor); CompletableFutureDemoDto demoDto=future.join(); System.out.println("in main"); System.out.println("id from demoDto is " + demoDto.getId()); }
执行结果,主线程一直被阻塞,一直等到用户线程返回
in CompletableFutureDemo
in main
id from demoDto is 1
Java启动新线程的几种方式(Runnable、Callable、CompletableFuture)
标签:com demo future 返回 引入 submit ace 阻塞 except
原文地址:https://www.cnblogs.com/Brake/p/13256469.html