即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

thread的状态

编程语言 afan249 29℃ 0评论

jvisualvm监控线程的几种状态

import com.afan.util.SleeepUtils;

public class ThreadState {

    public static void main(String[] args){
        new Thread(new TimeWaiting(),"TimeWaitingThread").start();
        new Thread(new Waiting(),"waitingThread").start();
        new Thread(new Blocked(),"blockedThread-1").start();
        new Thread(new Blocked(),"blockedThread-2").start();
    }
    //该线程不断地进行睡眠
    static class TimeWaiting implements Runnable{
        @Override
        public void run() {
            while(true){
                SleeepUtils.second(100);
            }
        }
    }
    //该线程在waiting。class实例上等待
    static class Waiting implements Runnable{
        @Override
        public void run() {
            while(true){
                synchronized (Waiting.class) {
                    try{
                        Waiting.class.wait();
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    //该线程在Bocked.class实例上加锁后 不会释放该锁
    static class Blocked implements Runnable{

        @Override
        public void run() {
            synchronized (Blocked.class) {
                while(true){
                    SleeepUtils.second(100);
                }
            }
        }

    }
}

转载请注明:CodingBlog » thread的状态

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情