5-19
本题要求主线程退出时,在main方法中所启动的线程t1
也要自动结束。
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new PrintTask());
t1.setDaemon(true);
t1.join();
System.out.println(Thread.currentThread().getName() + " end"); } }
5-21
本题目要求t1线程打印完后,才执行主线程main方法的最后一句System.out.println(Thread.currentThread().getName()+" end");
public class Main {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new PrintTask());
t1.start();
t1.join();
System.out.println(Thread.currentThread().getName()+" end"); } }