温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java线程状态举例分析

发布时间:2021-11-16 16:03:05 来源:亿速云 阅读:138 作者:iii 栏目:大数据

本篇内容主要讲解“Java线程状态举例分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java线程状态举例分析”吧!

Java线程状态举例分析

下面就一些场景分析锁和线程的状态

1、 线程1获得锁后不释放,就是运行状态(RUNNABLE),线程2就是休眠状态TIMED_WAITING (sleeping)

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                while(true){}
            }catch (Exception e){
            }
        }
    }
}
package com.example.demo.thread.threadstate;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(30000);
        }catch (Exception e){

        }
    }
}
"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001a51b800 nid=0x42b8 waiting on condition [0x000000001b47f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.demo.thread.threadstate.ThreadState2.run(ThreadState2.java:14)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001a51b000 nid=0x6d5c runnable [0x000000001b37e000]
   java.lang.Thread.State: RUNNABLE
        at com.example.demo.thread.threadstate.ThreadState1.run(ThreadState1.java:10)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

2、线程1获得锁后不释放,就是运行状态(RUNNABLE),线程2未获得锁就是阻塞状态(BLOCKED)

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                while(true){}
            }catch (Exception e){
            }
        }
    }
}
package com.example.demo.thread.threadstate;

import sun.misc.Unsafe;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(30000);
        }catch (Exception e){

        }
        System.out.println("ThreadState2---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState2---get---lock");
            }catch (Exception e){

            }
            ThreadStateTest.class.notify();
        }
        System.out.println("ThreadState2---end");
    }
}
"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001a51b800 nid=0x42b8 waiting for monitor entry [0x000000001b47f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.example.demo.thread.threadstate.ThreadState2.run(ThreadState2.java:21)
        - waiting to lock <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001a51b000 nid=0x6d5c runnable [0x000000001b37e000]
   java.lang.Thread.State: RUNNABLE
        at com.example.demo.thread.threadstate.ThreadState1.run(ThreadState1.java:10)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

3、线程1获得锁后sleep,就是休眠状态TIMED_WAITING (sleeping),线程2未获得锁就是阻塞状态(BLOCKED),这说明sleep不释放锁

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                System.out.println("ThreadState1---begin---sleep");
                Thread.sleep(30000);
                System.out.println("ThreadState1---end---sleep");
            }catch (Exception e){

            }
        }
    }
}
package com.example.demo.thread.threadstate;

import sun.misc.Unsafe;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        }catch (Exception e){

        }
        System.out.println("ThreadState2---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState2---get---lock");
            }catch (Exception e){

            }
            ThreadStateTest.class.notify();
        }
        System.out.println("ThreadState2---end");
    }
}
"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001aea0800 nid=0x33c waiting for monitor entry [0x000000001bdef000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.example.demo.thread.threadstate.ThreadState2.run(ThreadState2.java:17)
        - waiting to lock <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001ae86000 nid=0xb50 waiting on condition [0x000000001bcef000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.demo.thread.threadstate.ThreadState1.run(ThreadState1.java:11)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

4、线程1获得锁后wait,就是等待状态WAITING (on object monitor),线程2sleep就是休眠状态TIMED_WAITING (sleeping)

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                System.out.println("ThreadState1---begin---wait");
                ThreadStateTest.class.wait();
                System.out.println("ThreadState1---end---wait");
            }catch (Exception e){

            }
        }
    }
}
package com.example.demo.thread.threadstate;

import sun.misc.Unsafe;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        }catch (Exception e){

        }
        System.out.println("ThreadState2---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState2---get---lock");
                Thread.sleep(20000);
            }catch (Exception e){

            }
            ThreadStateTest.class.notify();
        }
        System.out.println("ThreadState2---end");
    }
}
"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001ae49800 nid=0x73c8 waiting on condition [0x000000001bd4f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.demo.thread.threadstate.ThreadState2.run(ThreadState2.java:18)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001ae3d000 nid=0x6d18 in Object.wait() [0x000000001bc4f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Object.wait(Object.java:502)
        at com.example.demo.thread.threadstate.ThreadState1.run(ThreadState1.java:11)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

5、线程1获得锁后wait,就是等待状态TIMED_WAITING (on object monitor),线程2sleep就是休眠状态TIMED_WAITING (sleeping)

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                System.out.println("ThreadState1---begin---wait");
                ThreadStateTest.class.wait(4000);
                System.out.println("ThreadState1---end---wait");
            }catch (Exception e){

            }
        }
    }
}
package com.example.demo.thread.threadstate;

import sun.misc.Unsafe;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        }catch (Exception e){

        }
        System.out.println("ThreadState2---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState2---get---lock");
                Thread.sleep(20000);
            }catch (Exception e){

            }
            ThreadStateTest.class.notify();
        }
        System.out.println("ThreadState2---end");
    }
}
"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001ac1b800 nid=0x6274 waiting on condition [0x000000001bb4f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at com.example.demo.thread.threadstate.ThreadState2.run(ThreadState2.java:18)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001ac1b000 nid=0x4654 in Object.wait() [0x000000001ba4f000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at com.example.demo.thread.threadstate.ThreadState1.run(ThreadState1.java:11)
        - locked <0x00000000d63ffac0> (a java.lang.Class for com.example.demo.thread.threadstate.ThreadStateTest)
        at java.lang.Thread.run(Thread.java:748)

6、线程1获得锁后wait,释放锁,线程2获得锁,释放锁后,线程1获得锁继续执行

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                System.out.println("ThreadState1---begin---wait");
                ThreadStateTest.class.wait(10000);
                System.out.println("ThreadState1---end---wait");
            }catch (Exception e){

            }
        }
    }
}
package com.example.demo.thread.threadstate;

import sun.misc.Unsafe;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        }catch (Exception e){

        }
        System.out.println("ThreadState2---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState2---get---lock");
                Thread.sleep(20000);
            }catch (Exception e){

            }
            ThreadStateTest.class.notify();
        }
        System.out.println("ThreadState2---end");
    }
}
ThreadState1---begin---lock
ThreadState1---get---lock
ThreadState1---begin---wait
ThreadState2---begin---lock
ThreadState2---get---lock
ThreadState2---end
ThreadState1---end---wait

7、线程2ThreadStateTest.class.notify()报错,在释放锁后不能notify

package com.example.demo.thread.threadstate;

public class ThreadState1 implements Runnable{
    @Override
    public void run() {
        System.out.println("ThreadState1---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState1---get---lock");
                System.out.println("ThreadState1---begin---wait");
                ThreadStateTest.class.wait();
                System.out.println("ThreadState1---end---wait");
            }catch (Exception e){

            }
        }
    }
}
package com.example.demo.thread.threadstate;

import sun.misc.Unsafe;

public class ThreadState2 implements Runnable {

    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        }catch (Exception e){

        }
        System.out.println("ThreadState2---begin---lock");
        synchronized (ThreadStateTest.class){
            try {
                System.out.println("ThreadState2---get---lock");
                Thread.sleep(20000);
            }catch (Exception e){

            }
        }
        ThreadStateTest.class.notify();
        System.out.println("ThreadState2---end");
    }
}
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
	at java.lang.Object.notify(Native Method)
	at com.example.demo.thread.threadstate.ThreadState2.run(ThreadState2.java:23)
	at java.lang.Thread.run(Thread.java:748)

到此,相信大家对“Java线程状态举例分析”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI