springboot集成线程池

springboot @async 线程自定义线程池

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.Commit;

/**
 * 
 * @author hemingzhhu
 * @version 2018-6-1
 *
 */
@Component
public class Threads {

	public static Random random = new Random();
	
	Logger log = LoggerFactory.getLogger(Threads.class);

    @Async("taskExecutor")
    public void doTaskOne() throws Exception {
        log.info("开始做任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("完成任务一,耗时:" + (end - start) + "毫秒");
    }

    @Async("taskExecutor")
    public void doTaskTwo() throws Exception {
        log.info("开始做任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("完成任务二,耗时:" + (end - start) + "毫秒");
    }

    @Async("taskExecutor")
    public void doTaskThree() throws Exception {
        log.info("开始做任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        log.info("完成任务三,耗时:" + (end - start) + "毫秒");
    }
	
}

2.直接调用就好

@RestController
public class ThreadController {

	 @Autowired
	    private Threads task;

	   @GetMapping("/aa")
	    public void test() throws Exception {

	        task.doTaskOne();
	        task.doTaskTwo();
	        task.doTaskThree();

	        Thread.currentThread().join();
	    }
	
}

springboot @async 使用Futrue以及定义超时加ThreadPoolTaskScheduler线程池的调用

1.

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * 
 * @author hemingzhhu
 * @version 2018-6-1
 *
 */
@Component
public class Threads {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	public static Random random = new Random();
	
	Logger log = LoggerFactory.getLogger(Threads.class);

	 @Async("taskExecutor")
	    public void doTaskOne() throws Exception {
	        log.info("开始做任务一");
	        long start = System.currentTimeMillis();
	        log.info(stringRedisTemplate.randomKey());
	        long end = System.currentTimeMillis();
	        log.info("完成任务一,耗时:" + (end - start) + "毫秒");
	    }

	    @Async("taskExecutor")
	    public void doTaskTwo() throws Exception {
	        log.info("开始做任务二");
	        long start = System.currentTimeMillis();
	        log.info(stringRedisTemplate.randomKey());
	        long end = System.currentTimeMillis();
	        log.info("完成任务二,耗时:" + (end - start) + "毫秒");
	    }

	    @Async("taskExecutor")
	    public void doTaskThree() throws Exception {
	        log.info("开始做任务三");
	        long start = System.currentTimeMillis();
	        log.info(stringRedisTemplate.randomKey());
	        long end = System.currentTimeMillis();
	        log.info("完成任务三,耗时:" + (end - start) + "毫秒");
	    }
	
	    
	    @Async("taskExecutor")
	    public Future<String> run1() throws Exception {
	        long sleep = random.nextInt(10000);
	        log.info("开始任务,需耗时:" + sleep + "毫秒");
	        Thread.sleep(sleep);
	        log.info("完成任务");
	        return new AsyncResult<>("test");
	    }
	    @Async("taskExecutor")
	    public Future<String> run2() throws Exception {
	        long sleep = 1000;
	        log.info("开始任务,需耗时:" + sleep + "毫秒");
	        Thread.sleep(sleep);
	        log.info("完成任务");
	        return new AsyncResult<>("test");
	    }
	    
}
2.
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.thread.util.Threads;

@RestController
public class ThreadController {

	 @Autowired
	    private Threads task;

	   @GetMapping("/aa")
	    public void test() throws Exception {

		   for (int i = 0; i < 50; i++) {
	            task.doTaskOne();
	            task.doTaskTwo();
	            task.doTaskThree();

	            if (i == 9999) {
	                System.exit(0);
	            }
	        }
	    }
	 Logger log = LoggerFactory.getLogger(ThreadController.class);
	 
	 @GetMapping("/bb")
	 public void test() throws Exception {
		 Future<String> futureResult = task.run1();
	     String result = futureResult.get(100, TimeUnit.SECONDS);
	     if(futureResult.isDone()) {
	    	 log.info("我在等待1");
	     }
	     log.info(result);
	     
	     Future<String> futureResult1 = task.run2();
	     String result1 = futureResult1.get(100, TimeUnit.SECONDS);
	     if(futureResult1.isDone()) {
			 log.info("我在等待2"+futureResult1.get());
		 }
	     log.info(result1);
	     
	 }
	
}
3、
 @EnableAsync
	    @Configuration
	    class TaskPoolConfig {

	        @Bean("taskExecutor")
	        public Executor taskExecutor() {
	            ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
	            executor.setPoolSize(20);
	            executor.setThreadNamePrefix("taskExecutor-");
	            return executor;
	        }

	    }
	 
	 @Bean("taskExecutor")
	 public Executor taskExecutor() {
	     ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
	     executor.setPoolSize(20);
	     executor.setThreadNamePrefix("taskExecutor-");
	     executor.setWaitForTasksToCompleteOnShutdown(true);
	     executor.setAwaitTerminationSeconds(60);
	     return executor;
	 }
	

4.

# Redis\u670D\u52A1\u5668\u5730\u5740
spring.redis.host=localhost
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
spring.redis.port=6379
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
spring.redis.password=
# \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09
spring.redis.timeout=50000
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09  
spring.redis.jedis.pool.max-active=8  
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09  
spring.redis.jedis.pool.max-wait=-1  
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5  
spring.redis.jedis.pool.max-idle=8  
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5  
spring.redis.jedis.pool.min-idle=0  



猜你喜欢

转载自blog.csdn.net/qq_37497275/article/details/80537013