setdaemon 方法_Java Thread类的最终void setDaemon(boolean thread_status)方法(带示例)...

setdaemon 方法

线程类最终void setDaemon(boolean thread_status) (Thread Class final void setDaemon(boolean thread_status))

  • This method is available in package java.lang.Thread.setDaemon(Boolean thread_status).

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

  • This method is used to set the current thread as 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 void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method does not raise an exception if the current thread can't modify this thread.

    如果当前线程无法修改此线程,则此方法不会引发异常。

Syntax:

句法:

    final void setDaemon(boolean thread_status){
    }

Parameter(s):

参数:

We pass only one object (thread_status) as a parameter in the method of the Thread. Here thread_status is of the boolean type so the value will be true or false if set true in setDaemon(true) method so it means this thread is a daemon thread and else set false in setDaemon(false) method so it means this thread is not a daemon thread.

我们仅在Thread方法中传递一个对象( thread_status )作为参数。 这里thread_status是布尔类型,因此,如果在setDaemon(true)方法中将true设置为true,则该值将为true或false,因此这意味着该线程是守护程序线程,而在setDaemon(false)方法中将false设置为false,因此意味着该线程不是守护程序线程。

Return value:

返回值:

The return type of this method is void, it does not return anything.

此方法的返回类型为void ,它不返回任何内容。

Java程序演示setDaemon()方法的示例 (Java program to demonstrate example of setDaemon() 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 SetDaemonThread extends Thread {
    //Override run() method of Thread class 
    public void run() {

        // To check the thread is daemon
        if (Thread.currentThread().isDaemon()) {

            //Code for Daemon thread 
            System.out.println(Thread.currentThread().getName() + " is a daemon thread");
        } else {
            System.out.println(Thread.currentThread().getName() + " is not a daemon thread");
        }
    }

    public static void main(String[] args) {
        // creating three object of the class SetThreadDaemon
        SetDaemonThread d1 = new SetDaemonThread();
        SetDaemonThread d2 = new SetDaemonThread();
        SetDaemonThread d3 = new SetDaemonThread();

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

        // By using start() method we will start execution of the thread
        d1.start();
        d2.start();
        d3.start();
    }
}

Output

输出量

E:\Programs>javac SetDaemonThread.java

E:\Programs>java SetDaemonThread
Thread-0 is not a daemon thread
Thread-1 is a daemon thread
Thread-2 is not a daemon thread


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

setdaemon 方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值