目录
1.含义
线程从创建到终止的过程
2.线程周期的六种状态(枚举State)
NEW新建状态
RUNNABLE运行状态
BLOCK阻塞状态
TIMED_WAITING有限时等待状态
WAITING无限时等待状态
TERMINATED终止状态
3.执行过程——代码展示
① 当开始使用new实例化Thread对象的时候——NEW新建状态
代码展示:
public static void main(String[] args) {
final Test8 test8 = new Test8();
/*新建状态*/
Thread t1 = new Thread(){
@Override
public void run() {
test8.test();
}
};
//获取状态
System.out.println("新建状态" + t1.getState());
}
结果:
新建状态NEW
② 对象调用.start()方法——RUNNABLE运行状态
代码展示
public static void main(String[] args) {
final Test8 test8 = new Test8();
/*新建状态*/
Thread t1 = new Thread(){
@Override
public void run() {
test8.test();
}
};
t1.start()
//获取状态
System.out.println("运行状态" + t1.getState());
}
结果:
运行状态RUNNABLE
③ 当线程处于RUNNABLE状态,如果竞争锁,但是没有持有锁,就会切换到BLOCK状态,如果竞争到锁,执行完成后,再切换到RUNNBALE运行状态;
代码展示:
package com.bjsxt.test;
public class Test11 {
public synchronized void test(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final Test11 test11 = new Test11();
//第一个子线程
Thread t1 = new Thread(){
@Override
public void run() {
test11.test();
}
};
t1.start();
//第二个子线程
Thread t2 = new Thread(){
@Override
public void run() {
test11.test();
}
};
t2.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获取状态
System.out.println("BLOCK状态" + t2.getState());
}
}
结果:
BLOCK状态BLOCKED
④当线程处于RUNNABLE状态,对象调用.sleep()方法,就会切换到TIMED_WAITING有限时等待状态,等到休眠结束,切换到RUNNBALE运行状态;
代码展示;
public class Test8 {
public synchronized void test(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final Test8 test8 = new Test8();
Thread t1 = new Thread(){
@Override
public void run() {
test8.test();
}
};
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("有限时等待状态" + t1.getState());
}
}
结果:
有限时等待状态TIMED_WAITING
⑤当线程处于RUNNABLE状态,对象调用.wait()方法,就会切换到WAITING无限时等待状态,需要调用notify()或notifyAll()方法后,切换到RUNNBALE运行状态;
代码展示;
package com.bjsxt.test;
public class Test6 {
public static void main(String[] args) {
final Class<Test6> tc = Test6.class;
Thread t1 = new Thread(){
@Override
public void run() {
synchronized (tc){
try {
tc.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t1.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("状态:\t" + t1.getState());
Thread t2 = new Thread(){
@Override
public void run() {
synchronized (tc){
tc.notify();
}
}
};
t2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("使用notify()恢复t1后:\t" + t1.getState());
}
}
结果:
状态: WAITING
使用notify()恢复t1后: TERMINATED
⑥ 进入TERMINATED终止状态条件:线程执行完成 / Error / Exception;
代码展示:
package com.bjsxt.test;
public class Test11 {
public synchronized void test(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final Test11 test11 = new Test11();
/*新建状态*/
Thread t1 = new Thread(){
@Override
public void run() {
test11.test();
}
};
t1.start();
t1.stop();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//获取状态
System.out.println("TERMINATED状态" + t1.getState());
}
}
结果:
TERMINATED状态TERMINATED
4.图片详解