线程的五大状态及常用方法

线程的五大状态

  1. 创建--new Thread()
  2. 就绪--调用start()
  3. 阻塞--sleep()、wait()
  4. 运行--CPU调度
  5. 死亡--线程中断或结束,一旦进入死亡状态,就不能再次启动

线程的基本方法

线程stop方法

package com.edgar.lesson03;
//测试stop方法
//1.建议线程正常停止--->利用次数,不建议死循环
//2.建议使用标志位--->设置一个标志位
//3.不要使用stop或者destory等过时或者jdk不建议使用的方法
public class TestStop implements Runnable {

    //1.设置一个标志位
    private boolean flag = true;

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

            System.out.println("run...thread" + i++);
        }
    }


    //2.设置一个公开的方法停止线程,转换标志位
    public void stop() {
        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("main"+i);
            if (i == 900) {
                //调用stop方法切换标志位,让线程停止
                testStop.stop();
                System.out.println("线程该停止了");
            }
        }
    }
}

线程休眠sleep()

package com.edgar.lesson03;

import java.text.SimpleDateFormat;
import java.util.Date;
//线程休眠
//模拟倒计时
public class TestSleep {

    public static void main(String[] args) {

        //打印当前系统时间
        Date date = new Date(System.currentTimeMillis());
        while(true){
            try {
                Thread.sleep(1000);
                System.out.println(new SimpleDateFormat("HH:mm:ss").format(date));
                date = new Date(System.currentTimeMillis());//更新当前时间
            } 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;
            }
        }

    }
}

线程礼让yeild()

package com.edgar.lesson03;

//线程礼让
//礼让不一定成功,看CPU心情
public class TestYield {

    public static void main(String[] args) {
        MyYield myYield = new MyYield();
        new Thread(myYield,"a").start();
        new Thread(myYield,"b").start();
    }
}
class MyYield implements Runnable{

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

线程强行执行jion()

package com.edgar.lesson03;
//线程强行执行
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 < 1000; i++) {
            if(i==200){
                thread.join();
            }
            System.out.println("主线程"+i);

        }
    }
}

线程优先级setPriority()

package com.edgar.lesson03;
//线程的优先级
//优先级高不一定先执行,看CPU心情
public class TestPriority {

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+"--->"+Thread.currentThread().getPriority());

        MyPriority myPriority = new MyPriority();

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

        t1.start();

        t2.setPriority(3);
        t2.start();

        t3.setPriority(7);
        t3.start();

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



    }
}

class MyPriority implements Runnable{

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

测试线程的状态getState()

package com.edgar.lesson03;
//观察测试线程的状态
public class TestState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("///////////");
        });

        Thread.State state = thread.getState();
        System.out.println(state);//NEW

        thread.start();//启动线程
        state = thread.getState();
        System.out.println(state);//RUNNABLE

        while(state!=Thread.State.TERMINATED){
            Thread.sleep(100);
            state = thread.getState();//更新状态
            System.out.println(state);
        }



    }
}

守护线程setDaemon()

package com.edgar.lesson03;

//守护线程:gc()、main()
public class TestDaemon {

    public static void main(String[] args) {
        God god = new God();
        You you = new You();
        Thread thread = new Thread(god);
        Thread thread2 = new Thread(you);//用户线程
        thread.setDaemon(true);//设置守护线程
        thread.start();
        thread2.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("永远开心的活着");
        }
        System.out.println("你结束了生命!!");
    }
}

高并发场景

多个线程对同一个资源进行操作会出现高并发场景

posted @ 2021-04-14 21:57  EdgarStudy  阅读(626)  评论(0编辑  收藏  举报