- 代码示例
package com.example.springbootthreaddemo.demo02;
import com.mysql.cj.util.TimeUtil;
import java.util.concurrent.TimeUnit;
public class Demo {
public static void main(String[] args) {
new Thread(()->{
while(true){
try {
TimeUnit.SECONDS.sleep(120);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"STATUS_01").start();
new Thread(()->{
while(true){
synchronized (Demo.class){
try {
Demo.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"STATUS_02").start();
new Thread(new BlockedDemo(),"BLOCKED-DEMO-01").start();
new Thread(new BlockedDemo(),"BLOCKED-DEMO-02").start();
}
static class BlockedDemo extends Thread{
@Override
public void run() {
synchronized (BlockedDemo.class){
while(true){
try {
TimeUnit.SECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
启动运行,然后在idea中的target目录下找到Demo.class ,右键 open in terminal, 执行jps查看Java进程pid, 对5444这个pid 执行jstack命令就可以看到Java进程的状态了
spring-boot-thread-demo\target\classes\com\example\springbootthreaddemo\demo0
2>jps
13168
11652 Jps
2532 Launcher
5444 Demo
13096
spring-boot-thread-demo\target\classes\com\example\springbootthreaddemo\demo0
2>jstack 5444
2021-11-21 22:22:19
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):
"DestroyJavaVM" #18 prio=5 os_prio=0 tid=0x00000000016ec800 nid=0x2738 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"BLOCKED-DEMO-02" #17 prio=5 os_prio=0 tid=0x000000001bb84000 nid=0x1e0 waiting for monitor entry [0x000000001c6bf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.example.springbootthreaddemo.demo02.Demo$BlockedDemo.run(Demo.java:46)
- waiting to lock <0x00000000d6721898> (a java.lang.Class for com.example.springbootthreaddemo.demo02.Demo$BlockedDemo)
at java.lang.Thread.run(Thread.java:748)
"BLOCKED-DEMO-01" #15 prio=5 os_prio=0 tid=0x000000001bb83000 nid=0x4064 waiting on condition [0x000000001c5be000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.example.springbootthreaddemo.demo02.Demo$BlockedDemo.run(Demo.java:46)
- locked <0x00000000d6721898> (a java.lang.Class for com.example.springbootthreaddemo.demo02.Demo$BlockedDemo)
at java.lang.Thread.run(Thread.java:748)
"STATUS_02" #13 prio=5 os_prio=0 tid=0x000000001bb70800 nid=0x4018 in Object.wait() [0x000000001c4bf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d63f8ef0> (a java.lang.Class for com.example.springbootthreaddemo.demo02.Demo)
at java.lang.Object.wait(Object.java:502)
at com.example.springbootthreaddemo.demo02.Demo.lambda$main$1(Demo.java:28)
- locked <0x00000000d63f8ef0> (a java.lang.Class for com.example.springbootthreaddemo.demo02.Demo)
at com.example.springbootthreaddemo.demo02.Demo$$Lambda$2/443308702.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"STATUS_01" #12 prio=5 os_prio=0 tid=0x000000001bb6c800 nid=0x34c0 waiting on condition [0x000000001c3bf000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at com.example.springbootthreaddemo.demo02.Demo.lambda$main$0(Demo.java:17)
at com.example.springbootthreaddemo.demo02.Demo$$Lambda$1/205797316.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
上面的状态可以看到: STATUS_01 线程的状态是TIMED_WAITING, 这是因为调用sleep是设置了超时时间 STATUS_02 线程的状态是WAITING, 调用wait方法时没有设置超时时间 BLOCKED-DEMO-01线程的状态是TIMED_WAITING ,该线程先抢到了锁,执行带超时时间的sleep方法,因此线程状态是TIMED_WAITING, BLOCKED-DEMO-02线程的状态是BLOCKED 由于BLOCKED-DEMO-01抢到了锁并且在sleep过程中,BLOCKED-DEMO-02线程处于锁等待状态中,因此它的线程状态是 BLOCKED. 上面几种线程状态都属于阻塞,但是区别如下:
调用带超时时间的wait sleep join LockSupport.park 方法时,线程状态是TIMED_WAITING 调用不带超时时间的wait sleep join LockSupport.park 方法时,线程状态是WAITING, 当线程获取锁失败,处于锁等待时,当前线程状态是BLOCKED
线程生命周期:5种线程状态 or 7种线程状态? 站在Java的角度来说,一共有七种线程状态,而对于操作系统层面来说,它没有新建和种植状态,只有就绪,运行,waiting ,timed_waiting, 阻塞五种状态。
线程启动做了哪些事情 thread.start() or thread.run() ? java是不能够创建线程的,Java创建线程本质上是调用了一个本地方法 start0(), 通过jvm 最终由OS创建出线程,当线程通过CPU的调度算法获取到CPU资源时,就可以真正的执行了。
|