您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Java中怎么实现线程状态的切换
## 目录
1. [线程状态概述](#线程状态概述)
2. [新建状态(NEW)](#新建状态new)
3. [可运行状态(RUNNABLE)](#可运行状态runnable)
4. [阻塞状态(BLOCKED)](#阻塞状态blocked)
5. [等待状态(WTING)](#等待状态waiting)
6. [计时等待状态(TIMED_WTING)](#计时等待状态timed_waiting)
7. [终止状态(TERMINATED)](#终止状态terminated)
8. [状态切换实战案例](#状态切换实战案例)
9. [状态监控与诊断](#状态监控与诊断)
10. [常见问题与解决方案](#常见问题与解决方案)
11. [最佳实践](#最佳实践)
12. [总结](#总结)
<a name="线程状态概述"></a>
## 1. 线程状态概述
Java线程在其生命周期中会经历6种不同的状态,这些状态在`java.lang.Thread.State`枚举中明确定义:
```java
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WTING,
TIMED_WTING,
TERMINATED
}
状态转换图示:
stateDiagram
[*] --> NEW
NEW --> RUNNABLE: start()
RUNNABLE --> TERMINATED: 执行完成/异常退出
RUNNABLE --> WTING: wait()/join()/park()
WTING --> RUNNABLE: notify()/notifyAll()/unpark()
RUNNABLE --> TIMED_WTING: sleep()/wait(timeout)/join(timeout)
TIMED_WTING --> RUNNABLE: 超时/唤醒
RUNNABLE --> BLOCKED: 竞争同步锁
BLOCKED --> RUNNABLE: 获取锁成功
Thread.getState()
返回Thread.State.NEW
// 方式1:继承Thread类
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
// 方式2:实现Runnable接口
Runnable task = () -> System.out.println("Runnable running");
// 创建线程对象
Thread thread1 = new MyThread();
Thread thread2 = new Thread(task);
Thread thread = new Thread(() -> {});
System.out.println(thread.getState()); // 输出 NEW
thread.start(); // 转换为RUNNABLE状态
Thread thread = new Thread(() -> {
// 线程执行体
});
thread.start(); // 进入RUNNABLE状态
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000); // 进入TIMED_WTING
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread.start();
System.out.println(thread.getState()); // RUNNABLE
synchronized
同步锁失败Object lock = new Object();
Thread t1 = new Thread(() -> {
synchronized (lock) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(() -> {
synchronized (lock) { // 此处会阻塞
System.out.println("获得锁");
}
});
t1.start();
Thread.sleep(100); // 确保t1先获得锁
t2.start();
Thread.sleep(100);
System.out.println(t2.getState()); // BLOCKED
Object.wait()
Thread.join()
LockSupport.park()
Object monitor = new Object();
Thread t = new Thread(() -> {
synchronized (monitor) {
try {
monitor.wait(); // 进入WTING
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
Thread.sleep(100);
System.out.println(t.getState()); // WTING
Thread.sleep(long)
Object.wait(long)
Thread.join(long)
LockSupport.parkNanos()
LockSupport.parkUntil()
Thread t = new Thread(() -> {
try {
Thread.sleep(2000); // TIMED_WTING
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t.start();
Thread.sleep(100);
System.out.println(t.getState()); // TIMED_WTING
Thread.stop()
(已废弃)Thread t = new Thread(() -> {});
t.start();
t.join(); // 等待线程结束
System.out.println(t.getState()); // TERMINATED
class Buffer {
private Queue<Integer> queue = new LinkedList<>();
private int capacity;
public Buffer(int capacity) {
this.capacity = capacity;
}
public synchronized void produce(int item) throws InterruptedException {
while (queue.size() == capacity) {
wait(); // 进入WTING
}
queue.add(item);
notifyAll(); // 唤醒消费者
}
public synchronized int consume() throws InterruptedException {
while (queue.isEmpty()) {
wait(); // 进入WTING
}
int item = queue.poll();
notifyAll(); // 唤醒生产者
return item;
}
}
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<?> future = executor.submit(() -> {
Thread.sleep(1000);
return "Done";
});
// 监控任务状态
while (!future.isDone()) {
System.out.println("任务执行中...");
Thread.sleep(200);
}
System.out.println("任务完成");
jstack <pid> > thread_dump.txt
"main" #1 prio=5 os_prio=0 tid=0x00007f4874009800 nid=0xa waiting on condition [0x00007f487b4a6000]
java.lang.Thread.State: TIMED_WTING (sleeping)
at java.lang.Thread.sleep(Native Method)
at com.example.App.main(App.java:10)
// 死锁示例
Object lock1 = new Object();
Object lock2 = new Object();
new Thread(() -> {
synchronized (lock1) {
try { Thread.sleep(100); } catch (Exception e) {}
synchronized (lock2) {
System.out.println("Thread1");
}
}
}).start();
new Thread(() -> {
synchronized (lock2) {
synchronized (lock1) {
System.out.println("Thread2");
}
}
}).start();
解决方案:
- 使用tryLock()
设置超时
- 统一加锁顺序
- 使用更高级的并发工具
优先使用并发工具类:
ExecutorService executor = Executors.newCachedThreadPool();
Future<String> future = executor.submit(() -> "result");
避免直接操作线程状态:
suspend()/resume()/stop()
合理设置线程优先级:
thread.setPriority(Thread.NORM_PRIORITY);
使用线程安全集合:
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
Java线程状态管理是并发编程的核心基础,本文详细分析了: - 6种线程状态的特性与转换条件 - 各种状态切换的代码实现方式 - 实际开发中的状态监控技巧 - 常见问题的解决方案 - 线程编程的最佳实践
掌握这些知识将帮助开发者编写出更健壮、高效的并发程序。
本文共计约9650字,详细覆盖了Java线程状态切换的各个方面。实际开发中应当根据具体场景选择合适的线程管理策略,并充分利用现代Java并发工具简化开发。 “`
注:由于篇幅限制,这里展示的是精简后的文章框架和核心内容示例。完整的9650字文章需要在此基础上扩展以下内容: 1. 每种状态的更详细解释和子状态分析 2. 更多实际应用场景的代码示例 3. 性能优化相关建议 4. 不同Java版本的特性差异 5. 更深入的状态机转换分析 6. 与操作系统线程状态的对比 7. 完整的故障排查案例等
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。