java线程的状态
线程的状态
线程的状态:
线程停止
- 不推荐使用JDK提供的stop()、destroy()方法 @Deprecated 【已废弃】
- 推荐线程自己停止下来
- 建议使用一个标志位进行终止变量:当flag=false,则终止线程运行
package com.yuanyu.thread;
//测试停止线程
/*
1. 推荐线程正常停止下来-->利用次数,不建议死循环
2. 建议使用标志位-->设置一个标志位
3. 不要使用stop或者destroy等过时或JDK不建议使用的方法
*/
public class TestStop implements Runnable{
//设置一个标志位
private boolean flag=true;
@Override
public void run() {
int i=0;
while (flag){
System.out.println("The thread is running"+i++);
}
}
//设置一个公开的方法停止线程,转换标志位
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 < 100; i++) {
System.out.println("main"+i);
if (i==90){
testStop.stop(); //转换标志位 让线程停止
System.out.println("线程该停止了");
}
}
}
}
线程休眠(sleep)
- sleep(时间)指定当前线程阻塞的毫秒数;一般为1000ms即1s
- sleep存在异常InterruptedException;
- sleep时间达到后线程进入就绪状态;
- sleep可以模拟网络延时:放大问题的发生性,倒计时等
- 每一个对象都有一个锁,sleep 不会释放锁;
十秒钟倒计时:
package com.yuanyu.thread;
//模拟倒计时
public class TestSleep {
public static void main(String[] args) {
try {
tenDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void tenDown() throws InterruptedException {
int num=10;
while (true){
Thread.sleep(100);
System.out.println(num--);
if (num<=0){
break;
}
}
}
}
获取当前系统时间:
package com.yuanyu.thread;
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();
}
}
}
}
程序运行结果:
打印当前系统时间思路:
-
new Date(); 传入获取时间的方法System.currentTimeMills();
-
写一个while(true)线程块在其中写入线程体
-
让线程休眠一秒钟(捕获异常)
格式化输出当前时间:new SimpleDateFormat("HH:mm:ss").format(Date实例化对象);
-
更新当前系统时间:Date实例化对象=new Date(System.currentTimeMills());
线程礼让(yield)
将线程从运行状态转化为就绪状态,礼让不一定可以成功
package com.yuanyu.thread;
import sun.awt.windows.ThemeReader;
//测试线程礼让 不一定会成功 看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()+"线程停止执行");
}
}
程序运行结果:
礼让成功a线程开始执行后没有停止执行,而是让b线程开始执行
线程强行执行(join)
Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞(强行插队,不讲道理)
package com.yuanyu.thread;
//测试join方法
public class TestJoin implements Runnable{
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("VIP来了"+i);
}
}
public static void main(String[] args) {
TestJoin testJoin = new TestJoin();
Thread thread = new Thread(testJoin);
thread.start();
for (int i = 0; i < 20; i++) {
if (i==8){
try {
thread.join(); //main线程阻塞
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("main"+i);
}
}
}
程序运行结果:当Thread线程执行结束,main的主线程的8才继续执行
观测线程状态
查看JDK帮助文档可知Thread.State有六种状态
可以通过thread.getState()获取线程状态
package com.yuanyu.thread;
//观察线程状态
public class TestState {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; 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); //Run
while (state != Thread.State.TERMINATED){ //只要线程不终止则一直输出状态
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
state = thread.getState(); //更新线程
System.out.println(state); //输出状态
}
}
}
程序运行结果:
特别注意:已经死亡的线程不能再次启动!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义