如题求问:
测试类如下: public class TestThread {
// @Test
// public void test(){
// MyList myList = new MyList();
// new ThreadA(myList).start();
// new ThreadB(myList).start();
// }
public static void main(String[] args) {
MyList myList = new MyList();
new ThreadA(myList).start();
new ThreadB(myList).start();
}
}
带有 sleep()的类如下: public class ThreadA extends Thread {
protected static Logger logger = LoggerFactory.getLogger(ThreadA.class);
private MyList list;
public ThreadA(MyList myList){
this.list=myList;
}
@Override
public void run() {
try {
for (int i = 0; i <10 ; i++) {
list.add();
logger.info("添加了"+(i+1)+"个元素");
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
用 @Test 启动测试程序,代码走到 sleep()就回停止,而用 main 启动就正常,这是为什么?
1
gaocc OP 多线程小白求问,日志看了下测试类先启动也都是 main 线程
|