创建线程和获取线程结果的几种方式
创建一个线程的方式:
创建线程的方式总体可以分为两大类:一个是依赖于Thread类, 一个是依赖于线程池。
依赖于Thread类的创建方式:
package concurrent.newThread; import java.util.concurrent.*; public class NewThreadByThread { public static void main(String[] args) throws ExecutionException, InterruptedException { //1 传入Runnable接口,lambda表达式的实现 Thread thread = new Thread(()->{ System.out.println("new thread by lambda implement runnable interface"); }); thread.start(); //1.1 传入Runnable接口的实现类NewThreadImplementRunnable, 注意该类并不是线程类 class NewThreadImplementRunnable implements Runnable { @Override public void run() { System.out.println("new thread by implement runnable interface"); } } NewThreadImplementRunnable tmpRunnableClass = new NewThreadImplementRunnable(); Thread thread2 = new Thread(tmpRunnableClass); thread2.start(); //2 通过继承Thread类并重写run方法 class NewThreadExtendsThread extends Thread { @Override public void run() { System.out.println("new thread by extend thread and override run method"); } } Thread thread1 = new NewThreadExtendsThread(); thread1.start(); //3 有返回值,传入callable接口的实现类,并利用FutureTask接受消息 class ImplementCallable implements Callable<String> { @Override public String call() throws Exception { System.out.println("new thread by implement callable interface"); return "call method return finished"; } } //FutureTask是一个实现Runnable接口的类,FutureTas内部有一个成员变量将callable接口的返回值保存起来,并通过提供get方法获取 FutureTask<String> futureTask = new FutureTask<>(new ImplementCallable()); Thread thread3 = new Thread(futureTask); thread3.start(); System.out.println(futureTask.get()); } }
测试结果:
new thread by lambda implement runnable interface
new thread by implement runnable interface
new thread by extend thread and override run method
new thread by implement callable interface
call method return finished
依赖于线程池的创建方式:
package concurrent.newThread; import java.util.concurrent.*; public class NewThreadByExecutorPool { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(3); //1 无返回值,传入Runnable接口的lambda表达式实现 executorService.submit(()->{ System.out.println("new thread by lambda implement runnable interface"); }); //1.1 无返回值 传入Runnable接口的实现类NewThreadImplementRunnable, 注意该类并不是线程类 class NewThreadImplementRunnable implements Runnable { @Override public void run() { System.out.println("new thread by class implement runnable interface"); } } executorService.submit(new NewThreadImplementRunnable()); //2.1 有返回值,通过线程池,传入实现callable接口的lambda表达式 Future<String> future = executorService.submit(()->{ System.out.println("new thread by executorService and lambda implement callable method"); return "second: call method return finished"; }); System.out.println(future.get()); //2.2 有返回值,通过线程池,传入Callable的实现类 class ImplementCallable1 implements Callable<String> { @Override public String call() throws Exception { System.out.println("new thread by executorService and class implement callable"); return "first: call method return finished"; } } future = executorService.submit(new ImplementCallable1()); System.out.println(future.get()); } }
测试结果:
new thread by lambda implement runnable interface
new thread by class implement runnable interface
new thread by executorService and lambda implement callable method
second: call method return finished
new thread by executorService and class implement callable
first: call method return finished
获取线程结果的方式:
第一种方式是往线程池传入一个callabe接口,通过future获取返回的结果;
第二种方式是往thread传入一个FutureTask, 通过FutureTask的get方法获取结果
第三种方式通过利用执行CompletableFuture的异步方法,通过CompletableFuture的get方法获取结果。
public class ThreadReturnValue { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(2); Future<String> future = executorService.submit(()-> { System.out.println("begin to do logic work"); return "get result by future\n"; }); System.out.println(future.get()); FutureTask<String> futureTask = new FutureTask<String>(()->{ System.out.println("begin to do logic work"); return "get result by futureTask\n"; }); Thread thread = new Thread(futureTask); thread.start(); System.out.println(futureTask.get()); CompletableFuture<String> future1 = CompletableFuture.supplyAsync(()->{ System.out.println("begin to do logic work"); return "get result by completableFuture\n"; }); System.out.println(future1.get()); } }
begin to do logic work
get result by future
begin to do logic work
get result by futureTask
begin to do logic work
get result by completableFuture
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了