Java Thread isDaemon()方法

java.lang.Thread.isDaemon() 方法测试该线程是否为守护线程。

1 语法

public final boolean isDaemon()

2 参数

3 返回值

如果该线程是守护线程此方法返回true;否则为false。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.lang.Thread.isDaemon()方法的例子
 */
import java.lang.*;

class adminThread extends Thread {

    adminThread() {
        setDaemon(true);
    }

    public void run() {
        boolean d = isDaemon();
        System.out.println("isDaemon = " + d);
    }
}

public class Demo {

    public static void main(String[] args) throws Exception {

        Thread thread = new adminThread();
        System.out.println("thread = " + thread.currentThread());
        thread.setDaemon(true);

        // this will call run() method
        thread.start();
    }
}

输出结果为:

thread = Thread[main,5,main]

热门文章

优秀文章