线程的特殊方法

一、休眠线程

public static void sleep(long millis)

class MyThread2 extends Thread{
    @Override
    public void run() {
        System.out.println("我是李刚,现在开始睡觉了...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("我醒了,现在开始学习!!");
    }
}
public class ThreadDemo2 {
    public static void main(String[] args) {
        MyThread2 myThread2 = new MyThread2();
        myThread2.start();
    }
}

二、加入线程:

public final void join()

class MyThread3 extends Thread {
    @Override
    public void run() {
        for (int i = 1; i <= 200; i++) {
            System.out.println(getName() + "-" + i);
        }
    }
}

public class ThreadDemo3 {
    public static void main(String[] args) throws InterruptedException {
        MyThread3 t1 = new MyThread3();
        MyThread3 t2 = new MyThread3();
        MyThread3 t3 = new MyThread3();


        t1.start();
        t1.join();

        t2.start();
        t3.start();
    }
}

三、礼让:

public static void yield()

class MyThread4 extends Thread{
    @Override
    public void run() {
        for(int i=1;i<=200;i++){
            System.out.println(getName()+"- "+i);
            Thread.yield();
        }
    }
}

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

        t1.start();
        t2.start();
    }
}

四、后台线程:

public final void setDaemon(boolean on)

/*
    后台线程:
        public final void setDaemon(boolean on)


    用户线程:优先级高于守护线程
    守护线程【后台线程】:当一个程序没有了用户线程,守护线程也就没有了
 */

class MyThread5 extends Thread{
    @Override
    public void run() {
        for(int i=1;i<=200;i++){
            System.out.println(getName()+"- "+i);
        }
    }
}

public class ThreadDemo5 {
    public static void main(String[] args) {
        MyThread5 t1 = new MyThread5();
        MyThread5 t2 = new MyThread5();
        MyThread5 t3 = new MyThread5();

        t1.setName("刘备");
        t2.setName("关羽");
        t3.setName("张飞");

        //将t2和t3线程设置为守护线程
        t2.setDaemon(true);
        t3.setDaemon(true);

        t1.start();
        t2.start();
        t3.start();
    }
}

五、中断线程:

public final void stop()
public void interrupt()

/*
    中断线程:
        public final void stop()
        public void interrupt()

    java所有的线程要想变成运行状态,必须经过抢cpu执行权

 */
class MyThread6 extends Thread{
    @Override
    public void run() {
        System.out.println("胡海祥准备睡觉了....睡足10秒钟");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("睡醒了,继续敲代码!!!");
    }
}

public class ThreadDemo6 {
    public static void main(String[] args) {
        MyThread6 t1 = new MyThread6();
        t1.start();

        try {
            Thread.sleep(5000);
//            t1.stop();
            t1.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

posted @ 2024-10-19 14:28  你的镁偷走了我的锌  阅读(15)  评论(0)    收藏  举报