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注解实现定时任务功能,不需要额外 ...
随机推荐
- CF375D Tree and Queries(dsu on tree)
思路 dsu on tree的板子,可惜人傻把 for(int i=fir[u];i;i=nxt[i]) 打成 for(int i=fir[u];i<=n;i++) 调了两个小时 这题要求维护& ...
- Java二进制指令
转自: http://www.blogjava.net/DLevin/archive/2011/09/13/358497.html 指令从0x00-0xc9 没有0xba 常量入栈指令 指令码 操作码 ...
- js 字符串加密解密
Welcome to jzzy.com
- 【译】第10节---数据注解-Key
原文:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.asp ...
- 51nod 1689 逛街(优先队列)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1689 题意: 题意: 枚举终点,这样就确定路上的花费,接下来只需要计算进 ...
- C++中substr函数的用法
#include<iostream> #include<string> using namespace std; int main(){ string str("12 ...
- [从零开始搭网站六]为域名申请免费SSL证书(https),并为Tomcat配置https域名所用的多SSL证书
点击下面连接查看从零开始搭网站全系列 从零开始搭网站 由于国内的网络环境比较恶劣,运营商流量劫持的情况比较严重,一般表现为别人打开你的网站的时候会弹一些莫名其妙的广告...更过分的会跳转至别的网站. ...
- 冒烟测试(smoke testing)
冒烟测试(smoke testing),就是开发人员在个人版本的软件上执行目前的冒烟测试项目,确定新的程序代码不出故障.冒烟测试的对象是每一个新编译的需要正式测试的软件版本,目的是确认软件基本功能正常 ...
- idea使用教程(1)
引言:本教程主要讲解一下常用的配置安装方法,不包含软件安装,按照以下教程配置后,可以直接用于生产环境. 参考网址:参考了尚硅谷关于idea的使用教学视屏 idea注册码地址:http://idea.l ...
- 小程序scss页面布局
html <view class="main"> <form bindsubmit="feedback"> <textarea c ...