多线程中Callable简单使用
Callable接口具备的特征如下
1.有简单的类型参数,与call()方法的返回类型相对应
2.声明了call方法,执行器运行任务时,该方法会被执行器执行,它必须返回声明中指定类型的对象
3.call()方法可以抛出任何一种校验异常,可以实现自己的执行器重载afterExecute方法来处理这些异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.java.test.callable; import lombok.extern.slf4j.Slf4j; import java.util.UUID; import java.util.concurrent.Callable; /** * @author yourheart * @Description * @create 2022-10-19 22:50 */ @Slf4j public class ThreadCallable implements Callable<String> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ @Override public String call() throws Exception { Thread.sleep( 5000 ); log.info( "call执行中..." ); return UUID.randomUUID().toString(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | package com.java.test.callable; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * @author yourheart * @Description * @create 2022-10-19 22:51 */ @Slf4j public class CallableTest { /** * 核心线程数 */ int corePoolSize = 3 ; /** * 最大线程数 */ int maximumPoolSize = 6 ; /** * 超过 corePoolSize 线程数量的线程最大空闲时间 */ long keepAliveTime = 2 ; /** * 以秒为时间单位 */ TimeUnit unit = TimeUnit.SECONDS; /** * 创建工作队列,用于存放提交的等待执行任务 */ BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>( 2 ); @Test /** * 执行器测试 */ public void actuatorTest() throws ExecutionException, InterruptedException { ThreadCallable threadCallable = new ThreadCallable(); ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, new ThreadPoolExecutor.AbortPolicy()) { @Override protected void afterExecute(Runnable r, Throwable t) { /** * 当call方法中执行有错误,可以在这里处理 */ // super.afterExecute(r, t); log.info( "afterExecute 任务执行结束了....:{}" ,t); } }; Future<String> submit = poolExecutor.submit(threadCallable); String result = submit.get(); log.info( "result:{}" ,result); poolExecutor.shutdown(); } public void test() throws ExecutionException, InterruptedException { ThreadCallable threadCallable = new ThreadCallable(); FutureTask<String> futureTask = new FutureTask<>(threadCallable); //启动线程,执行callable的业务 new Thread(futureTask).start(); //同步等待返回结果 String result = futureTask.get(); log.info( "输出线程的返回结果:{}" ,result); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通