spring集成线程池

springboot @async 线程自定义线程池

  1. import java.util.Random;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.scheduling.annotation.Async;
  5. import org.springframework.stereotype.Component;
  6. import org.springframework.test.annotation.Commit;
  7. /**
  8. *
  9. * @author hemingzhhu
  10. * @version 2018-6-1
  11. *
  12. */
  13. @Component
  14. public class Threads {
  15. public static Random random = new Random();
  16. Logger log = LoggerFactory.getLogger(Threads.class);
  17. @Async("taskExecutor")
  18. public void doTaskOne() throws Exception {
  19. log.info("开始做任务一");
  20. long start = System.currentTimeMillis();
  21. Thread.sleep(random.nextInt(10000));
  22. long end = System.currentTimeMillis();
  23. log.info("完成任务一,耗时:" + (end - start) + "毫秒");
  24. }
  25. @Async("taskExecutor")
  26. public void doTaskTwo() throws Exception {
  27. log.info("开始做任务二");
  28. long start = System.currentTimeMillis();
  29. Thread.sleep(random.nextInt(10000));
  30. long end = System.currentTimeMillis();
  31. log.info("完成任务二,耗时:" + (end - start) + "毫秒");
  32. }
  33. @Async("taskExecutor")
  34. public void doTaskThree() throws Exception {
  35. log.info("开始做任务三");
  36. long start = System.currentTimeMillis();
  37. Thread.sleep(random.nextInt(10000));
  38. long end = System.currentTimeMillis();
  39. log.info("完成任务三,耗时:" + (end - start) + "毫秒");
  40. }
  41. }

2.直接调用就好

  1. @RestController
  2. public class ThreadController {
  3. @Autowired
  4. private Threads task;
  5. @GetMapping("/aa")
  6. public void test() throws Exception {
  7. task.doTaskOne();
  8. task.doTaskTwo();
  9. task.doTaskThree();
  10. Thread.currentThread().join();
  11. }
  12. }

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

1.

  1. import java.util.Random;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.data.redis.core.StringRedisTemplate;
  6. import org.springframework.scheduling.annotation.Async;
  7. import org.springframework.scheduling.annotation.AsyncResult;
  8. import org.springframework.stereotype.Component;
  9. import java.util.concurrent.Future;
  10. /**
  11. *
  12. * @author hemingzhhu
  13. * @version 2018-6-1
  14. *
  15. */
  16. @Component
  17. public class Threads {
  18. @Autowired
  19. private StringRedisTemplate stringRedisTemplate;
  20. public static Random random = new Random();
  21. Logger log = LoggerFactory.getLogger(Threads.class);
  22. @Async("taskExecutor")
  23. public void doTaskOne() throws Exception {
  24. log.info("开始做任务一");
  25. long start = System.currentTimeMillis();
  26. log.info(stringRedisTemplate.randomKey());
  27. long end = System.currentTimeMillis();
  28. log.info("完成任务一,耗时:" + (end - start) + "毫秒");
  29. }
  30. @Async("taskExecutor")
  31. public void doTaskTwo() throws Exception {
  32. log.info("开始做任务二");
  33. long start = System.currentTimeMillis();
  34. log.info(stringRedisTemplate.randomKey());
  35. long end = System.currentTimeMillis();
  36. log.info("完成任务二,耗时:" + (end - start) + "毫秒");
  37. }
  38. @Async("taskExecutor")
  39. public void doTaskThree() throws Exception {
  40. log.info("开始做任务三");
  41. long start = System.currentTimeMillis();
  42. log.info(stringRedisTemplate.randomKey());
  43. long end = System.currentTimeMillis();
  44. log.info("完成任务三,耗时:" + (end - start) + "毫秒");
  45. }
  46. @Async("taskExecutor")
  47. public Future<String> run1() throws Exception {
  48. long sleep = random.nextInt(10000);
  49. log.info("开始任务,需耗时:" + sleep + "毫秒");
  50. Thread.sleep(sleep);
  51. log.info("完成任务");
  52. return new AsyncResult<>("test");
  53. }
  54. @Async("taskExecutor")
  55. public Future<String> run2() throws Exception {
  56. long sleep = 1000;
  57. log.info("开始任务,需耗时:" + sleep + "毫秒");
  58. Thread.sleep(sleep);
  59. log.info("完成任务");
  60. return new AsyncResult<>("test");
  61. }
  62. }
2.
  1. import java.util.concurrent.Future;
  2. import java.util.concurrent.TimeUnit;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import com.example.thread.util.Threads;
  9. @RestController
  10. public class ThreadController {
  11. @Autowired
  12. private Threads task;
  13. @GetMapping("/aa")
  14. public void test() throws Exception {
  15. for (int i = 0; i < 50; i++) {
  16. task.doTaskOne();
  17. task.doTaskTwo();
  18. task.doTaskThree();
  19. if (i == 9999) {
  20. System.exit(0);
  21. }
  22. }
  23. }
  24. Logger log = LoggerFactory.getLogger(ThreadController.class);
  25. @GetMapping("/bb")
  26. public void test() throws Exception {
  27. Future<String> futureResult = task.run1();
  28. String result = futureResult.get(100, TimeUnit.SECONDS);
  29. if(futureResult.isDone()) {
  30. log.info("我在等待1");
  31. }
  32. log.info(result);
  33. Future<String> futureResult1 = task.run2();
  34. String result1 = futureResult1.get(100, TimeUnit.SECONDS);
  35. if(futureResult1.isDone()) {
  36. log.info("我在等待2"+futureResult1.get());
  37. }
  38. log.info(result1);
  39. }
  40. }
3、
  1. @EnableAsync
  2. @Configuration
  3. class TaskPoolConfig {
  4. @Bean("taskExecutor")
  5. public Executor taskExecutor() {
  6. ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
  7. executor.setPoolSize(20);
  8. executor.setThreadNamePrefix("taskExecutor-");
  9. return executor;
  10. }
  11. }
  12. @Bean("taskExecutor")
  13. public Executor taskExecutor() {
  14. ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
  15. executor.setPoolSize(20);
  16. executor.setThreadNamePrefix("taskExecutor-");
  17. executor.setWaitForTasksToCompleteOnShutdown(true);
  18. executor.setAwaitTerminationSeconds(60);
  19. return executor;
  20. }

4.

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值