Java Thread类的最终boolean isDaemon()方法(带示例)

线程类最后一个布尔值isDaemon() (Thread Class final boolean isDaemon())

  • This method is available in package java.lang.Thread.isDaemon().

    软件包java.lang.Thread.isDaemon()中提供了此方法。

  • This method is used to check whether the current thread is a daemon thread.

    此方法用于检查当前线程是否是守护程序线程。

  • Daemon thread is the thread which runs in the background.

    守护程序线程是在后台运行的线程。

  • This method is not static so we cannot access this method with the class name too.

    此方法不是静态的,因此我们也无法使用类名访问此方法。

  • This method is final we can't override this method in child class.

    此方法是最终方法,我们不能在子类中覆盖此方法。

  • The return type of this method is boolean so it returns true if the thread is daemon else return false if the thread is user thread.

    此方法的返回类型为boolean,因此如果线程为守护程序,则返回true;如果线程为用户线程,则返回false。

Syntax:

句法:

    final boolean isDaemon(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the Thread.

我们不会在Thread方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is boolean, it returns true if this thread is daemon else return false.

此方法的返回类型为boolean ,如果此线程为守护程序,则返回true,否则返回false。

Java程序演示isDaemon()方法的示例 (Java program to demonstrate example of isDaemon() method)

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class IsThreadDaemon extends Thread {
    //Override run() method of Thread class 
    public void run() {

        // Code to check for daemon thread
        if (Thread.currentThread().isDaemon()) {

            //Display Daemon thread code
            System.out.println("Is thread " + getName() + "daemon?" + Thread.currentThread().isDaemon());
        } else {
            System.out.println("Not a Daemon thread" + getName());
        }
    }

    public static void main(String[] args) {

        // creating three object of the class IsThreadDaemon 
        IsThreadDaemon td1 = new IsThreadDaemon();
        IsThreadDaemon td2 = new IsThreadDaemon();
        IsThreadDaemon td3 = new IsThreadDaemon();

        // td2 is a daemon thread which is set by setDaemon(true) method
        td2.setDaemon(true);

        // By using start() method we will start execution of the thread
        td1.start();
        td2.start();
        td3.start();
    }
}

Output

输出量

E:\Programs>javac IsThreadDaemon.java

E:\Programs>java IsThreadDaemon
Not a Daemon threadThread-0
Not a Daemon threadThread-2
Is thread Thread-1daemon?true


翻译自: https://www.includehelp.com/java/thread-class-final-boolean-isdaemon-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值