Java启动新线程的几种方式(Runnable、Callable、CompletableFuture)
2021-04-25 22:29
标签:com demo future 返回 引入 submit ace 阻塞 except 输出结果 可以看到线程的运行没有阻塞当前线程 输出结果 Join会阻塞当前线程,一直等待自定义线程才返回。 在Runnable的例子中,Runnable接口有一个很大的缺陷就是run方法没有返回值定义,主线程无法获取到线程执行的结果。这个时候就需要Callable接口。 输出结果,如下所示,新启动的线程没有阻塞当前线程 get方法首先会阻塞主线程,等待当前线程执行结束才返回,且返回线程的执行结果。 CompletableFuture是jdk1.8引入的api,做了进一步的封装,用户线程无需实现Callable接口也能启动,且能够返回用户线程的执行结果 执行结果,可以看到,主线程没有被阻塞 执行结果,主线程一直被阻塞,一直等到用户线程返回 Java启动新线程的几种方式(Runnable、Callable、CompletableFuture) 标签:com demo future 返回 引入 submit ace 阻塞 except 原文地址:https://www.cnblogs.com/Brake/p/13256469.html一、实现Runnable接口
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
二、实现Callable接口
public class CallableDemo implements Callable
非阻塞调用
public static void main(String[] args) throws Exception {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
Future
in main
in callable demo
阻塞调用,且拿到结果
public static void main(String[] args) throws Exception {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
Future
三、CompletableFuture方式
public static CompletableFuture supplyAsync(Supplier supplier, Executor executor)
一个没有实现Callable的普通方法
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
in main
in CompletableFutureDemo
阻塞调用且获取结果
public static void main(String[] args) throws Exception {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
CompletableFuture
in CompletableFutureDemo
in main
id from demoDto is 1
文章标题:Java启动新线程的几种方式(Runnable、Callable、CompletableFuture)
文章链接:http://soscw.com/index.php/essay/79552.html