springboot项目线程使用
下面是一个demo:
public class TestThread { private static int nThreads =Runtime.getRuntime().availableProcessors() * 2 + 1; //创建的线程数理论最优值是cpu核数的2n+1 private static ExecutorService executors = Executors.newFixedThreadPool(nThreads, new ThreadFactory() { //创建线程池 private final String threadNamePrefix="thread_name_task_"; private final AtomicInteger count = new AtomicInteger(1);//原子性操作,保证每个线程数值的安全性 @Override
public Thread newThread(Runnable r) {
Thread t = new Thread(Thread.currentThread().getThreadGroup(),r,threadNamePrefix + count.getAndIncrement());
t.setDaemon(true);
return t;
}
}); public static void main(String[] args) { List<Future<String[]>> fList = new ArrayList<>(); for (int i = 0; i < 10; i++) {
final int nextInt = new Random().nextInt(100);
Future<String[]> f = executors.submit(new TestTask(nextInt)); fList.add(f);
} /*for (Future<String[]> future : fList) {
try {
String [] result = future.get();
System.out.println(result[0] + " , 结果 " + result[1]);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}
} System.out.println("main 线程结束 "); } static class TestTask implements Callable<String[]> { private int i ; public TestTask(int i){
this.i = i;
} @Override
public String[] call() throws Exception {
String result = i%2 == 0 ? "S" : "F";
// 业务处理逻辑
//Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "第" + i + "次任务");
return new String[] {Thread.currentThread().getName(),result};
}
}
}
线程异步执行结果:
springboot项目线程使用的更多相关文章
- springboot项目线程使用2
线程处理的一个实际例子: @Service public class SynAsynThreadTestServiceImpl implements SynAsynThreadTestService ...
- springboot项目 线程消费队列注入报错误空指针
背景: 在调用阿里云隐私保护有一个通话记录的回执消息是一个消费线程队列,这个还别人告诉我的,因为我根本没有看出来哪里是个线程了,然后我就把它当成普通的代码拿到返回值以后然后插入数据库 可是我这边该加的 ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- SpringBoot学习:在Interillj Idea上快速搭建SpringBoot项目
一.创建SpringBoot项目 二.导入Jar包(pom.xml) <?xml version="1.0" encoding="UTF-8"?> ...
- SpringBoot源码分析之---SpringBoot项目启动类SpringApplication浅析
源码版本说明 本文源码采用版本为SpringBoot 2.1.0BUILD,对应的SpringFramework 5.1.0.RC1 注意:本文只是从整体上梳理流程,不做具体深入分析 SpringBo ...
- SpringBoot项目集成Hystrix
Hystrix Hystrix是由Netflix开源的一个服务隔离组件,通过服务隔离来避免由于依赖延迟.异常,引起资源耗尽导致系统不可用的解决方案. 1.什么是服务熔断 服务熔断就是对该服务的调用 ...
- VisualVM分析与HelloWorld、springBoot项目
VisualVM分析与HelloWorld.springBoot项目 自从1995年第一个JDK版本JDKBeta发布,至今已经快25年,这些年来Java的框架日新月异,从最开始的Servlet阶段, ...
- 在liuunex下部署 springBoot项目
1.新建springBoot项目. 2.打包生成jar 3.丢到liunex丢到(/usr/local/software) 4.检查进程,ps -ef|grep java (java代表所有的java ...
- 【API知识】SpringBoot项目中@EnableXXX的原理
@EnableXX注解的使用场景 SpringBoot为开发人员提供了很多便利,例如如果想要定时功能,只要添加@EnableSchedule,即可配合@Schedule注解实现定时任务功能,不需要额外 ...
随机推荐
- A successful Git branching model——经典篇
A successful Git branching model In this post I present the development model that I’ve introduced f ...
- 8、nginx和tengine简介
练习: 使用nginx反向代理(rr调度)用户请求至两个以上的后端LAMP(按标准路径部署的有pma,wd),不管用户请求是什么内容都反向代理至后端服务器去,但是如果用户请求的是图片或者是html,就 ...
- git pull 提示 There is no tracking information for the current branch
在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...
- linux c/c++ 文件是否存在
linux c/c++ 文件是否存在 #include <unistd.h> int FileExist(const char* fname) { return access(fname, ...
- 关于ES6的一些新特性的学习
一.关于变量 ES5 1.只有全局作用域变量和函数作用域变量 2.“变量提升”(当程序进入一个新的函数时,会将该函数中所有的变量的声明放在函数开始的位置.仅仅会提升变量的声明,不会提升变量的赋值) E ...
- Java获得日期相差的天数
文章来源: http://www.jb51.net/article/75551.htm 这篇文章主要介绍了java计算两个时间相差天数的方法,感兴趣的小伙伴们可以参考一下: 问题描述: 输入:两个日期 ...
- windows下vue开发环境的搭建
一 介绍: vue.js是什么? Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库 ...
- mybatis-generator使用心得
通过web service给前端返回数据 首先后台先建表, 再针对表进行CRUD的各种sql, 然鹅,现在流行做法是使用mybatis,直接xml把sql融合了,什么事都有利弊,像我这样的手写sql党 ...
- 单细胞数据初步处理 | drop-seq | QC | 质控 | 正则化 normalization
比对 The raw Drop-seq data was processed with the standard pipeline (Drop-seq tools version 1.12 from ...
- Tomcat ----> 学习笔记
源码之几个常见类和接口的关系 在学习Servlet的时候经常见到以下几个合成单词和非合成单词:Servlet.GenericServlet.HttpServlet.它们之间有联系的.接下来我把它们的联 ...