同一个线程池执行不同类型的任务

编程入门 行业动态 更新时间:2024-10-27 14:24:54

同一个<a href=https://www.elefans.com/category/jswz/34/1771240.html style=线程池执行不同类型的任务"/>

同一个线程池执行不同类型的任务

1、同一个线程池可以执行不同的任务类型,也可以带返回值,也可以不带返回值的


import com.googlemon.util.concurrent.ThreadFactoryBuilder;
import com.vip.vman.result.BasicResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.*;@Slf4j
@RestController
@RequestMapping(value = "/vman/thread")
public class Thread2Controller {ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-get-all-test-%d").build();private  final ExecutorService executorService =  new ThreadPoolExecutor(3,60, 180L, TimeUnit.SECONDS,new LinkedBlockingQueue(),namedThreadFactory);/*** 带返回值的任务执行*/@RequestMapping(value = "/test1", method = RequestMethod.GET)public BasicResult executeTask4(String  id) throws InterruptedException, ExecutionException {BasicResult  basicResult = new BasicResult();Future<String> future=  executorService.submit(() -> {return id +  "rest1";});basicResult.setCodeMessage("0", future.get());return basicResult;}/*** 不带返回值的任务执行*/@RequestMapping(value = "/test2", method = RequestMethod.GET)public BasicResult executeTask43(String  id) throws InterruptedException, ExecutionException {BasicResult  basicResult = new BasicResult();executorService.execute(() -> {log.info("test2,id:{}", id);});basicResult.setCodeMessage("0", id);return basicResult;}}

多任务共用一个大的线程池还是每个任务单独使用线程池

多任务共用一个大的线程池还是每个任务单独使用线程池? - 掘金

更多推荐

同一个线程池执行不同类型的任务

本文发布于:2023-12-06 02:45:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1666214.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   不同类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!