您好,登錄后才能下訂單哦!
本篇內容主要講解“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線程狀態舉例分析”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。