刚学Java多线程时遇到的一个问题:
package chapter_one;
/**
* 继承 Thread 类 实现多线程
*/
public class MyThread extends Thread {
public MyThread() {
System.out.println("MyThread::" + Thread.currentThread().getName());
}
@Override
public void run() {
System.out.println("run::" + Thread.currentThread().getName());
}
}
在Junit中测试:
@Test
public void testCurrentThread() {
Thread thread = new MyThread();
thread.start();
}
输出:
MyThread::main
wtf?
Where is run::Thread-0
?
如此简单的一个测试程序居然都有问题?
在普通的类中测试则没有问题:
package chapter_one;
public class Main {
public static void main(String[] args) {
Thread thread = new MyThread();
thread.start();
}
}
那么就有理由推断是Junit的问题了。
Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其他线程自然也无法执行。
org\junit\runner\JUnitCore.java
public static void main(String... args) {
Result result = new JUnitCore().runMain(new RealSystem(), args);
System.exit(result.wasSuccessful() ? 0 : 1);
}
所以要想编写多线程Junit测试用例,就必须让主线程等待所有子线程执行完成后再退出,使用Thread.sleep()
或者Thread.join()
方法。
@Test
public void testCurrentThread() {
//返回代码正在被哪个线程调用的信息
// System.out.println(Thread.currentThread());
Thread thread = new MyThread();
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
输出:
MyThread::main
run::Thread-0
此处想必客官还有点疑问,为何构造函数的调用者是 main 线程呢?
说白了,构造函数本身就是个回调函数。