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上的实现方式就是采用了相关的技术),但是人家的名字叫做守护线程。