Skip to content

Commit

Permalink
spring多线程 TaskExecuter测试
Browse files Browse the repository at this point in the history
  • Loading branch information
huangjun0124 committed Dec 10, 2019
1 parent fb241b0 commit de47781
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 43 deletions.
80 changes: 40 additions & 40 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions src/main/java/com/wishuok/config/ExcutorConfig.java
@@ -0,0 +1,37 @@
package com.wishuok.config;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@EnableAsync
@Configuration
public class ExcutorConfig {
private static Logger logger = Logger.getLogger(ExcutorConfig.class);

@Bean(name = "asyncServiceExecutor")
public Executor asyncServiceExecutor() {
logger.info("start executor -->");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//设置核心线程数
executor.setCorePoolSize(50);
//设置最大线程数
executor.setMaxPoolSize(300);
//设置队列大小
executor.setQueueCapacity(300);
//配置线程池的前缀
executor.setThreadNamePrefix("async-service-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//设置空闲时间
executor.setKeepAliveSeconds(60);
//进行加载
executor.initialize();
return executor;
}
}
@@ -0,0 +1,47 @@
package com.wishuok.controller;

import com.wishuok.pojo.Role;
import com.wishuok.service.TaskExecuterTestService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/task")
@Api(value = "/task", tags = {"TaskExecuter测试"})
public class TaskExecuterTestController {

@Autowired
private TaskExecuterTestService taskExecuterTestService;

@RequestMapping(value = "/doTest", method = RequestMethod.GET)
@ResponseBody
public String doTest() {
StopWatch sc = new StopWatch();
sc.start();
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
List<Role> roles = new ArrayList<Role>();
int totalCount = 100000;
for (int i = 0; i < totalCount; i++) {
Role role = new Role();
role.setId(UUID.randomUUID().toString());
role.setRoleName("testRole_" + i);
role.setNote(ft.format(new Date()));
roles.add(role);
}
taskExecuterTestService.insertRoles(roles);
sc.stop();
return "测试完成,插入数据 "+totalCount+" 条,总耗时 " + sc.getTotalTimeMillis() + " 毫秒";
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/wishuok/service/IRoleService.java
Expand Up @@ -4,4 +4,6 @@

public interface IRoleService {
Role findRoleById(String id);

int insertRole(Role role);
}
12 changes: 12 additions & 0 deletions src/main/java/com/wishuok/service/TaskExecuterTestService.java
@@ -0,0 +1,12 @@
package com.wishuok.service;

import com.wishuok.pojo.Role;
import org.springframework.stereotype.Service;

import java.util.List;


public interface TaskExecuterTestService {

void insertRoles(List<Role> roles);
}
6 changes: 6 additions & 0 deletions src/main/java/com/wishuok/service/impl/RoleService.java
Expand Up @@ -33,6 +33,12 @@ public Role findRoleById(String id) {
role = redisTemplateSameSession(role);
return role;
}

@Override
public int insertRole(Role role) {
return roleMapper.insert(role);
}

private Role testJedisTemplate(Role role){
redisTemplate.opsForValue().set(role.getId(), role);
return (Role)redisTemplate.opsForValue().get(role.getId());
Expand Down
@@ -0,0 +1,54 @@
package com.wishuok.service.impl;

import com.wishuok.pojo.Role;
import com.wishuok.service.TaskExecuterTestService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Service
public class TaskExecuterTestServiceImpl implements TaskExecuterTestService {

private Logger logger = Logger.getLogger(this.getClass());

@Resource(name = "asyncServiceExecutor")
private ThreadPoolTaskExecutor taskExecutor;

@Autowired
private ApplicationContext applicationContext;

@Override
public void insertRoles(List<Role> roles) {
CountDownLatch countDownLatch = new CountDownLatch(1000);
for (int i = 0; i < roles.size(); i++) {
Role role = roles.get(i);
taskExecutor.execute(new Runnable() {
@Override
public void run() {
try {
RoleService roleService = applicationContext.getBean(RoleService.class);
int ret = roleService.insertRole(role);
System.out.println("插入Role[" + role.getRoleName()+"]结果: " + ret + ", 当前线程id: " + Thread.currentThread().getId());
} catch (Exception e) {
e.printStackTrace();
} finally {
countDownLatch.countDown(); //这个不管是否异常都需要数量减,否则会被堵塞无法结束
}
}
});
}
try {
countDownLatch.await(); //保证之前的所有的线程都执行完成,才会走下面的;
// 这样就可以在下面拿到所有线程执行完的集合结果
System.out.println("all roles insert done..........................................................");
} catch (Exception e) {
logger.error("阻塞异常");
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/c3p0.properties
@@ -1,11 +1,11 @@
jdbc.classPath = Z:\\CodingUse\\Softwares\\apache-maven-3.6.0\\repository\\mysql\\mysql-connector-java\\8.0.16\\mysql-connector-java-8.0.16.jar
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.109.128:3306/test?serverTimezone=UTC
jdbc.url=jdbc:mysql://192.168.109.130:3306/test?serverTimezone=UTC
jdbc.username=mysqluser
jdbc.password=root

c3p0.initPoolSize = 5
c3p0.maxPoolSize = 500
c3p0.maxPoolSize = 151
c3p0.minPoolSize = 5
c3p0.acquireIncrement = 10
c3p0.maxIdleTime = 60000
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/spring/spring-mvc.xml
Expand Up @@ -8,7 +8,7 @@
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

<!--设置Controller控制器的扫描地址-->
<context:component-scan base-package="com.wishuok.controller" />
<context:component-scan base-package="com.wishuok.controller, com.wishuok.config" />

<!-- 配置springmvc返回数据的数据格式,注意必须放在<mvc:annotation-driven>之前 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
Expand Down

0 comments on commit de47781

Please sign in to comment.