java线程状态有两种说法。
java线程状态:1、新建状态New;2、就绪状态Runnable;3、运行状态Running;4、阻塞状态Blocked;5、死亡状态Dead。
以上状态是一种概念性说法,参考https://www.runoob.com/note/34745
Thread.State是一个内部枚举类,定义了6个枚举常量,分别代表Java线程的6种状态。
这6种状态是在线程转储日志中会见到的。
注意,runnable包含了就绪和运行中;其他状态只能跟runnable进行转换,只有从runable才能走向terminated;
首先jps,查询有哪些java进程
然后用jstack,把此刻的线程日志保存到文件。
也可以直接打印到控制台
- "Thread-1" #13 prio=5 os_prio=0 tid=0x000002b9b6f6d800 nid=0x52e8 waiting for monitor entry [0x000000f8ff9fe000]
- java.lang.Thread.State: BLOCKED (on object monitor)
- at com.example.mybatisgenerate.thread.ThreadSyc$Task.run(ThreadSyc.java:24)
- - waiting to lock <0x000000076b66b4f0> (a [B)
- at java.lang.Thread.run(Thread.java:750)
priority
优先级的缩写,表名了当前线程的优先级,取值范围为[1-10],默认为 5。在虚拟机进行线程调度的时候会参考该优先级为线程分配计算资源,这个数值越低越有优先获取到计算资源,一般不设置直接使用默认的优先级NativeID
的缩写,对应JVM 虚拟机中线程映射在操作系统中的线程编号。我们可以使用 top 查看进程对应的线程情况进行相关映射。- public class ThreadSleep {
-
- public static void main(String[] args) throws InterruptedException {
- new Thread(() -> {
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }, "自定义线程").start();
-
- Thread.sleep(20000);
- }
- }
sleep方法让线程进入timed_waiting状态,日志括号里备注了是sleeping。
先说结论:当线程数大于线程池核心数,并且线程空闲,此时线程状态是 TIMED_WAITING (parking),超过线程存活时间,线程被回收。
代码说明:线程池核心数1,最大数2,队列大小1,空闲线程存活时间5s。连续执行三个任务,任务1在执行,任务2在队列里,队列满了---》增加线程数,任务3被执行。(这里面,因为任务睡眠时间长,任务2在队列里肯定是最后被执行的)
- public class ThreadSleepPool {
-
- public static ExecutorService executorService = new ThreadPoolExecutor(1, 2, 5000L, TimeUnit.MILLISECONDS,
- new LinkedBlockingQueue
(1), - new ThreadFactory() {
- AtomicInteger atomicInteger = new AtomicInteger();
-
- @Override
- public Thread newThread(Runnable r) {
- Thread thread = new Thread(r, "自定义线程" + atomicInteger.getAndIncrement());
- return thread;
- }
- });
-
- public static void main(String[] args) throws InterruptedException {
- executorService.execute(new Task("1"));
- executorService.execute(new Task("2"));
- executorService.execute(new Task("3"));
-
- Thread.sleep(20000);
- }
-
- public static class Task implements Runnable {
-
- String name;
-
- public Task(String name) {
- this.name = name;
- }
-
- @Override
- public void run() {
- try {
- System.out.println(name+"执行");
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- }
- }
刚开始,两个线程都在sleep,说明任务在执行。
过一会,线程处于空闲状态,空闲线程getTask得不到任务,就被挂起,parkNanos,变成TIMED_WAITING状态
再过一会,空闲线程消失了,已经被回收了。
- public class ThreadSync {
-
- private static byte[] lock = new byte[1];
-
- public static void main(String[] args) {
- final Thread task1 = new Thread(new Task());
- final Thread task2 = new Thread(new Task());
- task1.start();
- task2.start();
- }
- private static class Task implements Runnable {
- @Override
- public void run() {
- synchronized (lock) {
- int i = 0;
- while (true) {
- i++;
- }
- }
-
- }
- }
- }
线程0拿到同步锁在执行中,状态runnable。线程1堵塞状态,在 monitor Object ,监控对象
- public class ThreadDeadSync {
-
- public static byte[] lock1 = new byte[1];
- public static byte[] lock2 = new byte[1];
-
- public static void main(String[] args) {
- new Thread(() -> {
- synchronized (lock1) {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- synchronized (lock2) {
- System.out.println("执行不到");
- }
- }
- }, "自定义线程1").start();
- new Thread(() -> {
- synchronized (lock2) {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- synchronized (lock1) {
- System.out.println("执行不到");
- }
- }
- }, "自定义线程2").start();
- }
- }
两个线程都是Blocked
在最下方有死锁分析 。方法执行显示,线程2 locked d0,然后等待另一个锁 waiting to lock b8。线程1locked b8,waiting to lock d0。
- public class ThreadLock {
-
- private static ReentrantLock lock = new ReentrantLock();
-
- public static void main(String[] args) {
- final Thread task1 = new Thread(new Task());
- final Thread task2 = new Thread(new Task());
- task1.start();
- task2.start();
- }
- private static class Task implements Runnable {
- @Override
- public void run() {
- lock.lock();
- int i = 0;
- while (true) {
- i++;
- }
- }
- }
- }
等待锁的线程状态是 WAITING (parking),能看到lock方法最后执行了 LockSupport.park()
- public class ThreadDeadLock {
-
- private static ReentrantLock lock1 = new ReentrantLock();
- private static ReentrantLock lock2 = new ReentrantLock();
-
- public static void main(String[] args) {
- new Thread(() -> {
- lock1.lock();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- lock2.lock();
- }, "自定义线程1").start();
- new Thread(() -> {
- lock2.lock();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- lock1.lock();
- }, "自定义线程2").start();
- }
- }
两个线程状态都是 WAITING (parking)
最下方有死锁分析。这里没有像同步锁显示线程锁住了哪个对象,但是显示了在等待哪个。parking to wait for xxx
jstack pid |ps -5 "线程名"
nid=0x38a ,16进制,操作系统的线程编号。
用C语言的printf函数打印成十进制
或者用计算器
top -Hp 进程号,883是jps查处的一个java进程,查询进程下的各线程cpu占有率。
835 836转为16进制就是nid。