多线程02 sleep,join,daemon

join

点击查看代码
package it_06;

public class Demo7 {
    public static void main(String[] args) {
        MyThread t1 =new MyThread("A");
        MyThread t2 =new MyThread("B");
        MyThread t3 =new MyThread("C");

//        System.out.println(t1.getPriority());

//        t1.setPriority(1);
//        t2.setPriority(5);
//        t3.setPriority(10);

        t1.start();
        try {
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        t2.start();
        t3.start();

    }
}

sleep(MyThread)
点击查看代码
package it_06;

public class MyThread extends Thread {
    MyThread(){}
    MyThread(String name){
        super(name);
    }

    @Override
    public void run() {
        for(int i=0;i<100;i++){
            System.out.println(getName()+": "+i);
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
        }
    }
}

daemon
点击查看代码
package it_06;

public class Demo8 {
    public static void main(String[] args) {
        MyThread t1 =new MyThread();
        MyThread t2 =new MyThread();

        t1.setName("A");
        t2.setName("B");
        Thread.currentThread().setName("C");
        t1.setDaemon(true);
        t2.setDaemon(true);
        t1.start();
        t2.start();
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+": "+i);
        }
    }
}

posted @ 2025-04-25 20:28  lfqyj  阅读(8)  评论(0)    收藏  举报