Thread之线程状态和suspend()

线程状态

在这里插入图片描述

在这里插入图片描述

验证线程状态

import lombok.SneakyThrows;
/**
 * @author layman
 */
public class Demo08 {
    public static void main(String[] args) throws InterruptedException {
        Demo08Service service = new Demo08Service();
        Demo08ThreadA t1 = new Demo08ThreadA(service);
        Demo08ThreadA t2 = new Demo08ThreadA(service);
        //t1的线程状态:NEW
        System.out.println("t1的线程状态:" + t1.getState());
        t1.start();
        //t1的线程状态:RUNNABLE
        System.out.println("t1的线程状态:" + t1.getState());
        //确保t1线程先启动
        Thread.sleep(500L);
        //t1的线程状态:TIMED_WAITING
        System.out.println("t1的线程状态:" + t1.getState());
        t2.start();
        //t2的线程状态:RUNNABLE? 为什么不是BLOCKED?奇怪?
        System.out.println("t2的线程状态:" + t2.getState());
        Thread.sleep(5000L);
        //t1的线程状态:TERMINATED
        System.out.println("t1的线程状态:" + t1.getState());

        Demo08ThreadB t3 = new Demo08ThreadB(new Object());
        t3.start();
        Thread.sleep(100L);
        //t3的线程状态:WAITING
        System.out.println("t3的线程状态:" + t3.getState());
    }
}
class Demo08Service{
    public synchronized void doSomething() throws InterruptedException {
        Thread.sleep(3000L);
    }
}
class Demo08ThreadA extends Thread{
    private Demo08Service service;
    public Demo08ThreadA(Demo08Service service){
        this.service = service;
    }
    @SneakyThrows
    @Override
    public void run() {
        service.doSomething();
    }
}
class Demo08ThreadB extends Thread{
    private Object lock;
    public Demo08ThreadB(Object lock){
        this.lock = lock;
    }
    @SneakyThrows
    @Override
    public void run() {
        synchronized (lock){
            lock.wait();
        }
    }
}

suspend()

import lombok.Data;
/**
 * suspend()是一个过时方法。
 * 它会使线程进入暂停状态,如果方法有synchronized修饰,那么它会持有锁,造成死锁,同时也会导致共享数据不同步问题
 * @author layman
 * @date 2021/2/5
 */
public class ThreadSuspend {
    private static demo01 demo01 = new demo01();

    public static void main(String[] args) {
        ThreadSuspend ts = new ThreadSuspend();
        //设置一个线程A
        Thread t1 = new Thread(() -> {
            while (true) {
                ts.doSomething();
            }
        });
        t1.setName("AAA线程");
        t1.start();
        //设置一个线程B
        Thread t2 = new Thread(() -> {
            while (true) {
                ts.doSomething();
            }
        });
        t2.setName("BBB线程");
        t2.start();
    }
    public void doSomething(){
        demo01.printString();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
@Data
class demo01{
    private String name = "layman";
    private String password = "12345";

    public synchronized void printString(){
        System.out.println(Thread.currentThread().getName()+"---name:"+name+" ,password"+password);
        if("AAA线程".equals(Thread.currentThread().getName())){
            Thread.currentThread().suspend();
        }

    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值