其他分享
首页 > 其他分享> > 试试多线程

试试多线程

作者:互联网

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"); } }

 

标签:main,currentThread,Thread,试试,t1,new,多线程,public
来源: https://blog.csdn.net/qq_51482719/article/details/121667017