如何在Java中創建用戶定義的守護程序線程? [重復]

[英]How to create user defined Daemon thread in Java? [duplicate]


This question already has an answer here:

這個問題在這里已有答案:

Can anyone tell me how we can create Daemon thread in Java?

誰能告訴我如何在Java中創建守護進程線程?

I mean the syntax and how it can be used and modified.

我的意思是語法以及如何使用和修改它。

1 个解决方案

#1


1  

JVM garbage collection thread is a typical Daemon Thread, and you can create daemon thread just like the normal thread and call such thread setDaemon(true) and here i make a simple demo:

JVM垃圾收集線程是一個典型的守護程序線程,你可以像普通線程一樣創建守護程序線程並調用這樣的線程setDaemon(true),在這里我做一個簡單的演示:

/**
 * Created by crabime on 11/10/16.
 */
public class DaemonTest extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++){
            System.out.println(getName() + "  " + i);
        }
    }

    public static void main(String[] args) {
        DaemonTest d = new DaemonTest();
        d.setDaemon(true);
        d.start();
        try {
            Thread.sleep(200);//after 200 million seconds, main thread ends and no matter DaemonTest thread run to the end or not, it will stop 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/11/10/72529b11bd24db17dd5d3e5b2e099d1d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com