调试时,子线程无缘无故终止了

自己写了个函数,开了个主线程,然后在主线程上面开了个子线程,但是在调试的时候,总是无缘无故的子线程终止了,没有任何缘由。

后来同事点拨才知道,我们调试的时候,因为主线程终止了,所以子线程你在调试的时候也终止了,并不是代码的问题。

 如果想调试,需要阻塞5分钟,这样就可以看到子线程的运行了。

代码如下:

复制代码
 //3.添加定时任务
    @Scheduled(cron = "0 0 1 * * ?")
        public String getHiveData() throws Exception {
            final String uuid = UUID.randomUUID().toString();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -1); //得到前一天
            Date date = calendar.getTime();
            DateFormat df = new SimpleDateFormat(DATE_FORMAT_SHORT_SIMPLE.getValue());
            System.out.println(df.format(date));

 String dt=df.format(date);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    queryHiveData(dt,uuid);
                } catch (Exception e) {
                    log.error("meet queryHiveData exception", e);
                }
            }
        });

        try {
            Thread.sleep(5*60*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return uuid;
    }
复制代码
复制代码
   private String queryHiveData(String dt,String uuid) throws Exception {

//            final AtomicInteger successCount = new AtomicInteger();
//        final AtomicInteger errorCount = new AtomicInteger();
        Map<String, String> statusMap = new HashMap<>(2);
        AsyncTalosClient talos = null;
        try {
            Engine engine = Engine.Presto;
            String dsn = "hlog";
            talos = new AsyncTalosClient(username, password);
            //必须开启session
            talos.openSession();

            //强引用,虚拟机栈结束之后会自动回收
            List<Map<String, String>> statusList = new ArrayList<>();
            //软引用包装
            SoftReference<List<Map<String, String>>> softReference = new SoftReference<>(statusList);
            concurrentHashMap.put(uuid, softReference);
return null;
}
复制代码

请注意这两段代码: 之前在没有阻塞主线程就是下面的代码的时候,调试的时候,子线程每次都会执行到:Engine engine = Engine.Presto;

就终止了 无缘无故,无法跟踪,后来加了下面的阻塞的代码:

 try {
            Thread.sleep(5*60*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

子线程就可以继续调试了,当部署生产的时候,就把这个阻塞去掉,就行了;

 

posted @   aspirant  阅读(968)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示