栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程再学习(6篇博客)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程再学习(6篇博客)

一,线程池的拒绝策略

Java ThreadPoolExecutor的拒绝策略CallerRunsPolicy的一个潜在的大坑_明明如月的技术博客-CSDN博客_callerrunspolicy

 

 二,Java ThreadPoolExecutor的拒绝策略

Java ThreadPoolExecutor的拒绝策略_明明如月的技术博客-CSDN博客

 

 三,ThreadPoolTaskExecutor和ThreadPoolExecutor区别

ThreadPoolTaskExecutor和ThreadPoolExecutor区别_背景模版-CSDN博客_threadpooltaskexecutor

四,springboot中@Async多线程注解使用

springboot中@Async多线程注解使用_背景模版-CSDN博客

 ①:根据当前服务器cpu内核,计算出核心线程数和最大线程数

②:用的线程池还是spring框架下的线程池ThreadPoolTaskExecutor

③:最大线程数和核心线程数这里保持一致

 

 五,案例

SpringBoot异步多线程调用注解@Async使用和CountDownLatch配合使用案例_背景模版-CSDN博客

 

 

 

@Autowired
    private Test test;
    @Override
    public List findByMonth() {
        List list = new ArrayList<>();
        for (int i =1;i<=12;i++){
            int count= baseMapper.findByMonth(i);
            list.add(count);
        }
        CountDownLatch countDownLatch = new CountDownLatch(6);
        System.out.println("开始执行多线程任务1111111111:::"+System.currentTimeMillis());
        for (int i =0;i<=5;i++){
            test.doTaskThree(countDownLatch,i);
 
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程继续执行222222222222222:::::"+Thread.currentThread().getName());
        return list;
 
 
 
 
@Component
public class Test {
    //注意这个多线程方法的类一定要加@Component注解,拿给spring容器管理
    @Async
    public void doTaskThree(CountDownLatch countDownLatch,int i) {
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("第00" + i + "完成任务,耗时:" + (end - start) + "毫秒,线成名为::" + Thread.currentThread().getName());
        countDownLatch.countDown();
    }
 
}

 

 

 

 还有一种就是使用多线程中带返回的 Future结果来进行主线程的控制,大概如下,可供参考。

@Async("getTaskExector")
    public Future doTaskThree(int i) {
        long start = System.currentTimeMillis();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("第00" + i + "完成任务,耗时:" + (end - start) + "毫秒,线成名为::" + Thread.currentThread().getName());
        return new AsyncResult("SUCUESS");
    }
 
 
 
 
 
System.out.println("开始执行多线程任务1111111111:::"+System.currentTimeMillis());
        List> list1 = new ArrayList<>();
        for (int i =0;i<=5;i++){
            Future stringFuture = test.doTaskThree(i);
            list1.add(stringFuture);
        }
        boolean flag = false;
        while (!flag){
            for (Future future:list1){
                try {
                    String s = future.get();
                    if (s =="成功"){
                        flag = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("主线程继续执行222222222222222:::::"+Thread.currentThread().getName());
 
六,CountDownLatch、Semaphone学习

CountDownLatch、Semaphone学习_chenkaibsw的专栏-CSDN博客

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号