線程狀態

下面就一些場景分析鎖和線程的狀態java

一、 線程1得到鎖後不釋放,就是運行狀態(RUNNABLE),線程2就是休眠狀態TIMED_WAITING (sleeping)ide

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)

二、線程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)

三、線程1得到鎖後sleep,就是休眠狀態TIMED_WAITING (sleeping),線程2未得到鎖就是阻塞狀態(BLOCKED),這說明sleep不釋放鎖3d

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)

四、線程1得到鎖後wait,就是等待狀態WAITING (on object monitor),線程2sleep就是休眠狀態TIMED_WAITING (sleeping)code

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)

五、線程1得到鎖後wait,就是等待狀態TIMED_WAITING (on object monitor),線程2sleep就是休眠狀態TIMED_WAITING (sleeping)blog

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)

六、線程1得到鎖後wait,釋放鎖,線程2得到鎖,釋放鎖後,線程1得到鎖繼續執行get

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

七、線程2ThreadStateTest.class.notify()報錯,在釋放鎖後不能notifyit

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)

總結:io

線程的狀態主要有BLOCKED、WAITING (on object monitor)、TIMED_WAITING (on object monitor)、TIMED_WAITING (sleeping)分別對應上圖中的各個方法,sleep不釋放鎖,wait釋放鎖,釋放的是synchronized得到的鎖,釋放鎖後須要從新得到鎖,只有得到鎖才能夠wait和notifyclass

相關文章
相關標籤/搜索