普通网友 2018-03-26 10:39 采纳率: 0%
浏览 1209
已结题

JVM 主线程退出,程序退出

关于JVM退出的问题,找到的资料都是
正常情况下当所有的non-deamon线程都结束时,JVM退出

但自己测试了一下,发现方法 startTest尽管创建了10个线程,但要是我取消了end.await,主线程结束,JVM就退出了,创建的线程也不会执行。

问题在于,通过日志可以了解,其实创建的子线程也是Non-daemon线程,那为什么JVM在还有Non-daemon线程时,也会推出呢?

代码如下,可以通过是否注释end.await调试

package com.unionpay.content.cache;



import java.util.concurrent.CountDownLatch;

import org.junit.Test;

import net.sf.ehcache.Element;

public class EhcacheTest {
    CountDownLatch start;
    CountDownLatch end;
    int threadsNum=10;
    int startThead=3;

    @Test
    public void startTest() throws ClassNotFoundException, InterruptedException {
        start=new CountDownLatch(startThead);
        end=new CountDownLatch(threadsNum);
        for (int i = 0; i < threadsNum; i++) {
            Thread thread=new Thread(new Runnable() {

                public void run() {
                    try {
                        ehcacheTest();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        System.out.println(Thread.currentThread().getName()+" exist error ");
                    }               
                }
            },"thread"+i);

            thread.start();

            start.countDown();

        }

        end.await();

        System.out.println("this is end");

    }



    public void ehcacheTest() throws InterruptedException {
        System.out.println("Deamon thread"+Thread.currentThread().isDaemon());
        System.out.println(Thread.currentThread().getName()+" is already");
        start.await();
        String key="a";


        Element element=EhcacheUtil.get(key);
        if (element == null) {
            System.out.println(Thread.currentThread().getName()+" get element is null");
            String result=serviceTest(key);
            EhcacheUtil.put(key, result, 5);
        }else{
            System.out.println(Thread.currentThread().getName()+" get element has something");
            String aString=(String) EhcacheUtil.get(key).getObjectValue();
            System.out.println("aString is:"+aString);
        }

        end.countDown();


    }


    public String serviceTest(String key) {
        int count=1;
        count++;
        System.out.println(Thread.currentThread().getName()+"get into serviceTest "+count+" times");
        return ""+count;
    }
}

  • 写回答

2条回答 默认 最新

  • huageng1 2018-03-27 06:57
    关注

    不会呀,你确定自己创建的那些线程是没有执行的吗,你在ehcacheTest()方法中加一个休眠看看Thread.currentThread().sleep(10000l);,如果线程没有执行结束,虚拟机肯定是还在运行的

    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀