189 8069 5689

Java线程状态举例分析

本篇内容主要讲解“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线程状态举例分析”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


新闻名称:Java线程状态举例分析
URL地址:http://cdxtjz.cn/article/ipchco.html

其他资讯