spring boot 实现配置一个线程池 多线程 并发 操作实例 代码介绍 代码

713 阅读3分钟

目录

介绍

1.做一个配置类

2.应用线程池的类

3.调用类

4.启动类

5.调用测试

6.日志


介绍

  • 完成多线程需要
  • 1.一个配置类
  • 2.一个调用类
  • 3.使用调用类
  • 4.调用
  • 5.启动
  • 6.查看
  • ok

 

1.做一个配置类

package com.super.conf;
 
import java.util.concurrent.Executor;
 
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;
 
@Configuration
@ComponentScan("com.super.service")
@EnableAsync
public class ThreadConfig  {
    /**
     * 执行需要依赖线程池,这里就来配置一个线程池
     * 
     * @return
     */
 
    // 当池子大小小于corePoolSize,就新建线程,并处理请求
    // 当池子大小等于corePoolSize,把请求放入workQueue(QueueCapacity)中,池子里的空闲线程就去workQueue中取任务并处理
    // 当workQueue放不下任务时,就新建线程入池,并处理请求,如果池子大小撑到了maximumPoolSize,就用RejectedExecutionHandler来做拒绝处理
    // 当池子的线程数大于corePoolSize时,多余的线程会等待keepAliveTime长时间,如果无请求可处理就自行销毁
 
    @Bean("getExecutor")
    public Executor getExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        executor.setCorePoolSize(1000);
        //设置最大线程数
        executor.setMaxPoolSize(10000);
        //线程池所使用的缓冲队列
        executor.setQueueCapacity(7500);
        //设置线程名
        executor.setThreadNamePrefix("service-Async");
        //设置多余线程等待的时间,单位:秒
        //executor.setKeepAliveSeconds();
        // 初始化线程
        executor.initialize();
        return executor;
    }
}
  • 加上如下注解

@Configuration
@ComponentScan("com.supermap.testCtr")
@EnableAsync

 

2.应用线程池的类

package com.super.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class ServiceF {

	@Async("getExecutor")
	public void get01() {
		System.out.println("执行任务1");
	}
	
	@Async("getExecutor")
	public void get02() {
		System.out.println("执行任务2");
	}

        @Async("getExecutor")
        public Future<String> execute(String req) {
		 
		return new AsyncResult<>(req);
	}
}
  • 给方法加上@Async("getExecutor")注解,加到类上 就全类的方法都是异步的多线程了
  • 有需要可以吧返回的对象更换 int list map  等等

 

3.调用类

package com.super.clienttest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.super.service.ServiceF;

@Controller
public class AsyncCtr {

	@Autowired
	private ServiceF sf;

	// 无返回值
	@RequestMapping("/getfTest")
	@ResponseBody
	public void getfTest() {
		for (int i = 0; i < 100; i++) {
			sf.get01();
			sf.get02();
		}
	}

	//有返回值可以吧线程池放大些
	@RequestMapping("/zszExecute")
	@ResponseBody
	public String zszExecute(HttpServletRequest req) {
		String url = req.getParameter("url") == null ? "" : req.getParameter("url");
		Future<String> future = sf.execute(url);
		return future.get();
	}
}

 

4.启动类

package com.supermap;

import org.apache.log4j.BasicConfigurator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.supermap.testCtr.InitInif;

/**
 * 项目启动类
 * 
 * @author yushen
 *
 */
@SpringBootApplication
public class Provider_App {
	
	/**
	 * 项目启动入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		
		SpringApplication.run(Provider_App.class, args);
		
	}
	
}
  • 不用任何修饰

5.调用测试

ip端口 找yml 配置文件看一下是多少就写多少

http://localhost:9999/getfTest

 

6.日志

2019-06-24 17:04:32.715 INFO  org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-06-24 17:04:32.717 INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started
2019-06-24 17:04:32.721 INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 4 ms
执行任务2
执行任务2
执行任务1
执行任务1
执行任务1
执行任务2
执行任务2
执行任务1
执行任务1
执行任务1
执行任务2
执行任务1
执行任务2
执行任务1
执行任务2
执行任务1
执行任务2
执行任务1
执行任务2
...
  • 好了,现在已经实现异步多线程了
  • 他没有按照12,12,12,12 的方式执行及正确了
  • ok

 

 

 

如果有需要也可以使用带返回值的

package com.super.async;

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

@Service
public class serviceI {

	@Async("getExecutor")
	public Future<String> getf1() {
		return new AsyncResult<String>("1"); 
	}
	
	@Async("getExecutor")
	public Future<String> getf2() {
		return new AsyncResult<String>("1"); 
	}
}
  • public Future<String> getf1() {


    return new AsyncResult<String>("1"); 

  • 用future 和 asyncResult 实现 

 

jmeter测试

 

  • 1秒钟轻松支持众多访问

ok

 

 

 

 

持续更新