线程状态

线程一共有五大状态

 1.创建状态  new

 2.就绪状态 调用start()方法

 3.运行状态 获取cpu资源

 4.阻塞状态 线程休眠

 5.死亡状态 线程执行完毕

 

停止线程

//测试停止线程
//1.建议线程正常停止-->利用册数,不建议死循环
//2.建议使用标志位-->设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法

public class TestStop implements Runnable{

    //设置标志位
    private boolean flag =true;

    @Override
    public void run() {
        int i=0;

        while (flag){
            System.out.println("线程开始-----------"+i++);
        }
    }

    //自己设置一个停止方法
    public  void stop1(){
       this.flag =false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
            System.out.println("这个是主函数线程--"+i);
            if (i==900){
//调用方法改变标志位让线程停止 testStop.stop1(); } } } }

 

网络延时模拟倒计时(sleep)

每个对象都有一把锁,sleep不会释放锁

//模拟倒计时
public class TestSleep2 {

    public static void main(String[] args) {
        try {
            tenDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    public static void  tenDown() throws InterruptedException {
        int num =10;

        while (true){
            Thread.sleep(1000);
            System.out.println(num--);
            if (num<=0){
                break;
            }
        }

    }
}

 

礼让线程 (yield)

//测试礼让线程
//礼让不一定成功,看cpu心情
public class TestYield {
    public static void main(String[] args) {

        MyYield my = new MyYield();

        new Thread(my,"a").start();
        new Thread(my,"b").start();

    }
}

class  MyYield implements  Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程开始");
         Thread.yield();//线程礼让
        System.out.println(Thread.currentThread().getName()+"线程结束");
    }
}

 

线程插队 (join)

//测试join线程 就好比插队
public class TestJoin implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println("插队线程来了"+i);
        }
    }
    public static void main(String[] args) throws InterruptedException {

        TestJoin testJoin = new TestJoin();
        Thread thread = new Thread(testJoin);
        thread.start();

        for (int i = 0; i < 100; i++) {
            if (i==50){
                thread.join();//插队
            }
            System.out.println("主线程"+i);
        }
    }
}

 

设置优先级  优先级高的不一定先执行,优先级指是先执行的比列

//测试线程的优先级 优先级从1-10 默认为5
public class TestProiority {
    public static void main(String[] args) {
        //主线程默认优先级
        System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority());

        MyProiority myProiority = new MyProiority();

        Thread t1 = new Thread(myProiority);
        Thread t2 = new Thread(myProiority);
        Thread t3 = new Thread(myProiority);
        Thread t4 = new Thread(myProiority);

        //设置优先级在启动
        t1.start();

        t2.setPriority(1);
        t2.start();

        t3.setPriority(10);
        t3.start();

        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();
        

    }
}

class MyProiority implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority());
    }
}

 

守护线程

//测试守护线程
//上帝守护线程  只要用户线程执行完毕之后,不用等待守护线程执行完毕
public class TestDaemon {
    public static void main(String[] args) {
        God god = new God();
        You you = new You();

        Thread thread = new Thread(god);
        thread.setDaemon(true);//默认是false来表示用户线程, true为守护线程
        thread.start();//守护线程开始

        new Thread(you).start();//你  用户线程启动
    }
}

//上帝
class God implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("上帝保佑着你");
        }
    }
}

//
class You implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 36500; i++) {
            System.out.println("你活着的第"+i+"天");
        }
        System.out.println("====goodbye! world========");
    }
}

 

posted @ 2022-05-23 15:19  不想当将军的好士兵  阅读(33)  评论(0)    收藏  举报