Java多线程随笔(2):线程状态

线程状态

 

线程停止

建议线程正常停止,不建议死循环

建议使用标志位

不要使用stop()或destroy()等JDK不建议使用的方法

public class TestStop implements Runnable{

    private boolean flag = true;

    @Override
    public void run() {
        int i = 0;
        while(flag){
            System.out.println("Thread"+i);
        }
    }

    public void stop(){
        flag = false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();

        for (int i = 0; i < 10; i++) {
            System.out.println("Main"+i);
            if(i == 5){
                testStop.stop();
                System.out.println("It's time for thread to stop");
            }
        }
    }
}

 

线程休眠

sleep(时间) 指定当前线程阻塞的毫秒数;

sleep存在异常InterruptedException;

sleep时间达到后线程进入就绪状态;

sleep不会释放锁;

作用:模拟网络延迟,放大问题的发生性;

举例:模拟倒计时

//倒计时10秒
public class TestSleep {

    public static void main(String[] args) {
        countDown();
    }

    public static void countDown(){
        int num = 10;

        while (num>0){
            try {
                Thread.sleep(1000);
                num--;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 

线程礼让

yield()让当前线程暂停,线程从运行状态变为就绪状态;

CPU重新调度,礼让不一定成功;

举例:

public class TestYield implements Runnable{

    public static void main(String[] args) {
        TestYield testYield = new TestYield();
        new Thread(testYield,"a").start();
        new Thread(testYield,"b").start();
    }

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

 

线程强制执行

join()合并线程,其他线程阻塞,待此线程执行完毕,再执行其他线程;

举例:

public class TestJoin implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("我必在main10之前执行完"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new TestJoin());
        thread.start();

        for (int i = 0; i < 20; i++) {
            if(i == 10){
                thread.join();
            }
            System.out.println("main"+i);
        }
    }
}

 

观测线程状态

举例:

public class TestState implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程即将结束");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new TestState());

        //创建状态变量
        Thread.State state = thread.getState();
        //New状态
        System.out.println(state);
        //启动线程
        thread.start();
        //Run状态
        state = thread.getState();
        System.out.println(state);
        //输出状态,指导线程终止
        while (state != Thread.State.TERMINATED){
            Thread.sleep(1000);
            state = thread.getState();
            System.out.println(state);
        }
    }
}

 

线程的优先级

线程的优先级用数字表示,范围是1~10:

  • Thread.MIN_PRIORITY  = 1;
  • Thread.NORM_PRIORITY = 5;
  • Thread.MAX_PRIORITY = 10;

获取优先级的方法:

  • Thread.getPriority()

改变优先级的方法:

  • Thread.setPriority(int xxx);

先设置优先级,再启动线程!

 

守护(daemon)线程

现成分为用户线程守护线程;

虚拟机必须确保用户线程执行完毕,不需要等待守护线程执行完毕;

守护线程包括后台记录操作日志,监控内存,垃圾回收等;

举例:上帝与人类 

public class TestDaemon {

    public static void main(String[] args) {
        Thread god = new Thread(new God());
        Thread person = new Thread(new Person());

        //将上帝线程设为守护线程
        god.setDaemon(true);

        god.start();
        person.start();
    }
}
//上帝
class God implements Runnable{
    @Override
    public void run() {
        while (true){
            System.out.println("God bless you forever...");
        }
    }
}
//人类
class Person implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("I am live.");
        }
        System.out.println("Bye, World...");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值