首页 > 编程语言 > 详细

Java 学习笔记之 线程isAlive方法

时间:2017-10-13 23:26:17      阅读:264      评论:0      收藏:0      [点我收藏+]

isAlive方法:

 

 方法isAlive()功能是判断当前线程是否处于活动状态。

活动状态就是线程启动且尚未终止,比如正在运行或准备开始运行。

public class IsAliveThread extends Thread {
    public IsAliveThread() {
        System.out.println("begin");
        System.out.println("Thread.currentThread().getName() : " + Thread.currentThread().getName());
        System.out.println("Thread.currentThread().isAlive() : " + Thread.currentThread().isAlive());
        System.out.println("this.getName() : " +  this.getName());
        System.out.println("this.isAlive() : " + this.isAlive());
        System.out.println("end");

    }

    @Override
    public void run() {
        System.out.println("run begin");
        System.out.println("Thread.currentThread().getName() : " + Thread.currentThread().getName());
        System.out.println("Thread.currentThread().isAlive() : " + Thread.currentThread().isAlive());
        System.out.println("this.getName() : " +  this.getName());
        System.out.println("this.isAlive() : " + this.isAlive());
        System.out.println("run end");

    }
}

public class ThreadRunMain {
    public static void main(String[] args) {
        testIsAliveThread();
    }
    public static void testIsAliveThread(){
        IsAliveThread ist = new IsAliveThread();
        Thread th = new Thread(ist);
        System.out.println("Main begin th isAlive = " + th.isAlive());
        th.start();
        System.out.println("Main end th isAlive = " + th.isAlive());
    }
}

运行结果:

技术分享

 

Java 学习笔记之 线程isAlive方法

原文:http://www.cnblogs.com/AK47Sonic/p/7663583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!