真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java線程狀態(tài)舉例分析

本篇內(nèi)容主要講解“Java線程狀態(tài)舉例分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Java線程狀態(tài)舉例分析”吧!

創(chuàng)新互聯(lián)建站是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設(shè)公司,自成立以來公司不斷探索創(chuàng)新,始終堅(jiān)持為客戶提供滿意周到的服務(wù),在本地打下了良好的口碑,在過去的十余年時(shí)間我們累計(jì)服務(wù)了上千家以及全國(guó)政企客戶,如葡萄架等企業(yè)單位,完善的項(xiàng)目管理流程,嚴(yán)格把控項(xiàng)目進(jìn)度與質(zhì)量監(jiān)控加上過硬的技術(shù)實(shí)力獲得客戶的一致稱譽(yù)。

Java線程狀態(tài)舉例分析

下面就一些場(chǎng)景分析鎖和線程的狀態(tài)

1、 線程1獲得鎖后不釋放,就是運(yùn)行狀態(tài)(RUNNABLE),線程2就是休眠狀態(tài)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獲得鎖后不釋放,就是運(yùn)行狀態(tài)(RUNNABLE),線程2未獲得鎖就是阻塞狀態(tài)(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,就是休眠狀態(tài)TIMED_WAITING (sleeping),線程2未獲得鎖就是阻塞狀態(tài)(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,就是等待狀態(tài)WAITING (on object monitor),線程2sleep就是休眠狀態(tài)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,就是等待狀態(tài)TIMED_WAITING (on object monitor),線程2sleep就是休眠狀態(tài)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獲得鎖繼續(xù)執(zhí)行

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()報(bào)錯(cuò),在釋放鎖后不能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)

到此,相信大家對(duì)“Java線程狀態(tài)舉例分析”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!


本文題目:Java線程狀態(tài)舉例分析
文章分享:http://weahome.cn/article/ipchco.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部