一:线程的状态
线程的状态,在Thread类中的State枚举中
public enum State {
NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED;
}
1.1:验证: NEW, RUNNABLE, TERMINATED状态
1、业务类
public class MyThread extends Thread{
public MyThread(){
System.out.println("MyThread构造方法中"+Thread.currentThread().getName()+"线程的状态为:"+Thread.currentThread().getState());
System.out.println("MyThread构造方法中"+getName()+"线程的状态为:"+getState());
}
@Override
public void run() {
System.out.println("MyThread的run()中"+getName()+"的状态 "+Thread.currentThread().getState());//myThread线程: RUNNABLE
}
}
2、测试类
public class Demo {
public static void main(String[] args) throws InterruptedException {
MyThread t1 = new MyThread();
t1.start();
Thread.sleep(1000);
System.out.println(t1.getName()+"的状态为: "+t1.getState());
}
}
3、控制台
MyThread构造方法中main线程的状态为:RUNNABLE
MyThread构造方法中Thread-0线程的状态为:NEW
MyThread的run()中Thread-0的状态 RUNNABLE
Thread-0的状态为: TERMINATED
1.2:验证TIMED_WAITING状态
1、业务类
public class MyThread extends Thread{
@Override
public void run() {
try {
System.out.println("begin sleep");
TimeUnit.SECONDS.sleep(10);
System.out.println("end sleep");
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
2、测试类
public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
try {
System.out.println(Thread.currentThread().getName()+"①线程的状态为: "+Thread.currentThread().getState()); //RUNNABLE
Thread.sleep(10000); //10s
System.out.println(Thread.currentThread().getName()+"②线程的状态为: "+Thread.currentThread().getState()); //RUNNABLE
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1");
t1.start();
Thread.sleep(1000);
System.out.println(t1.getName()+"③线程的状态为: "+t1.getState());
TimeUnit.SECONDS.sleep(10);
System.out.println(t1.getName()+"④线程的状态为: "+t1.getState());
}
}
3、控制台
t1①线程的状态为: RUNNABLE
t1③线程的状态为: TIMED_WAITING
t1②线程的状态为: RUNNABLE
t1④线程的状态为: TERMINATED
1.3:验证BLOCKED的状态
BLOCKED状态
出现在某个线程在等待锁的时候
1、业务类
public class MyService {
public static synchronized void method(){
try {
System.out.println("线程名为:"+Thread.currentThread().getName()+"进入了锁");
TimeUnit.SECONDS.sleep(10);//10s, 在指定时间内,不会释放锁!
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
2、测试类
public class Demo {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> MyService.method(),"t1");
Thread t2 = new Thread(() -> MyService.method(),"t2");
t1.start();
//让t1线程先启动!
Thread.sleep(1000);
t2.start();
Thread.sleep(1000); //必须让主线程休眠一下,不然t2下面输出的太快了,是t2抢锁之前的状态RUNNABLE!
System.out.println("t2线程的状态为:"+t2.getState());
}
}
3、控制台
线程名为:t1进入了锁
t2线程的状态为:BLOCKED
线程名为:t2进入了锁
1.4:验证WAITING状态
1、业务类
public class MyService {
private Object lock = new Object();
public void waitMethod(){
try {
synchronized (lock){
lock.wait(); //会释放锁!
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
2、测试类
public class Demo {
public static void main(String[] args) throws InterruptedException {
MyService myService = new MyService();
Thread t1 = new Thread(() -> myService.waitMethod());
t1.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("线程t1的状态为: "+t1.getState());
}
}
3、控制台
线程t1的状态为: WAITING
最后:来自一个虽然帅,但是菜的cxy