junit引发的多线程思考


public class VCenterPerformanceTest {
     private static final Log log = LogFactory
               .getLog(VCenterPerformanceTest.class);
     private static long counter = 0;
     private static long numCounter = 0;

     class Collector implements Callable<Object> {

     private Task task;
          public Collector(Task task) {
               this.task = task;
          }

          @Override
          public Object call() throws Exception {
               task.do();
               try{
               Thread.sleep(1000)
                }catch(Exception e){
                   }               
                numCounter++;

               return new metricData;
          }
     }

     @SuppressWarnings("rawtypes")
     @Test
     public void testVCenterPerformance() {
          int taskNumber = 100;
          ScheduledExecutorService pool = Executors
                    .newScheduledThreadPool(taskNumber);
          List<Collector> tasks = new ArrayList<Collector>();
          for (int i = 0; i < taskNumber; ++i) {
                    tasks.add(new Collector(new Task()));
          }
          List<Future> futures = new ArrayList<Future>();
          for (Collector task : tasks) {
               futures.add(pool.submit(task));
          }
          for (Future future : futures) {
               try {
                    future.get();
               } catch (Exception e) {
                    e.printStackTrace();
               }
          }
          if (numCounter != 0) {
               log.info("Everage time cost:" + counter / numCounter
                         + "| success task number:" + numCounter);
          } else {
               log.info("No task excutes successfully.");
          }
     }
}



如果把future的等待去掉,那么函数 testVCenterPerformance()结束运行后,程序会立即终止。
这里pool管理的子线程还在运行,但是系统不会等待子线程运行结束,而是立即结束。
但是我们将相同的代码通过main函数来运行,则main结束后,系统会等待子线程的结束。

开始还比较疑惑,但是后来分析一下就感觉很简单了。
一切的程序都是通过main开始的,所以junit test也是通过main开始的,只不过main被封装到junit的框架中。
我们来看下源代码:
junit.textui.TestRunner
 public static void main (String[] args) {
      TestRunner aTestRunner = new TestRunner();
      try {
        TestResult r = aTestRunner.start(args);
       if (!(r.wasSuccessful()))
          System.exit (1);
       System.exit(0);
      } catch (Exception e) {
       System.err.println(e.getMessage());
        System.exit(2);
      }
   }


其实junit是将test作为参数传递给了TestRunner的main函数。并通过main函数进行执行。test函数在main中执行。如果test执行结束,那么main将会调用 System. exit (0);
即使还有其他的线程在运行,main也会调用 System. exit (0);
System. exit ()是系统调用,通知系统立即结束jvm的运行,即使jvm中有线程在运行,jvm也会停止的。所以会出现之前的那种情况。其中 System. exit (0);的参数如果是0,表示系统正常退出,如果是非0,表示系统异常退出。

java的线程分为两种:正常线程和后台线程。
最大的区别就是系统会等待正常线程结束,但是如果正常线程全部结束,系统就会退出。这时即使有后台线程在运行,系统也会退出。也就是系统不会等待后台线程结束。
后台线程一般是为了给程序提供公共服务而建立起来的。如果程序的其他模块都结束了,那么这种服务也没有存在的必要了。
在Linux中也有类似的线程(虽然不清楚实现,但是感觉java在linux上的实现方式就是采用了相关的技术),但是人家的名字叫做守护线程。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值