1. Test 方法中启动线程, 没有执行完就直接推出了
使用 Thread.join() 让主线程等待子线程执行结束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Test public void test() throws InterruptedException { Thread thread = new Thread(new Runnable() { public void run() { while (true) { System.out.println("hello"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); thread.start(); thread.join(); }
|