简体   繁体   English

守护进程线程未按预期工作

[英]Daemon thread not working as expected

package main.components;

import java.io.Serializable;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class MainSnoozerx implements Runnable, Serializable {

    private static final long serialVersionUID = 1L;
    private static int min = 0;
    static Thread mnz = new Thread(new MainSnoozerx());
    private long convertedToMiliSec = 0l;
    private Scanner scn = new Scanner(System.in);

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try{
            do{
            System.out.println("Enter minutes to snooze..");
            min = scn.nextInt();
            }while(min<0);

        convertedToMiliSec = TimeUnit.MINUTES.toMillis(min);
        try {
            Thread.sleep(convertedToMiliSec);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Alarm Now!!!");
        }catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        mnz.setDaemon(true);
        mnz.start();
    }

}

Can anyone tell me what am I doing wrong/missing? 谁能告诉我我在做什么错/想念吗? My program just terminates when I run it without printing the syso even once. 我的程序在运行时就终止了,甚至没有打印过syso。 I expect the code to run endlessly being a daemon thread and the user just sets the mins once and the snooze goes on forever... 我希望代码可以作为守护进程线程无休止地运行,并且用户只需设置一次分钟,然后贪睡就会一直持续下去...

You've got it the wrong way around, the JVM will keep running while there is at least one non-daemon thread alive. 您遇到了错误的方法,当至少有一个非守护线程处于活动状态时,JVM将继续运行。 The main thread is not a daemon thread and if no other non-daemon threads are created before the main thread exits the JVM will exit. main线程不是守护程序线程,如果在主线程退出之前未创建其他非守护程序线程,则JVM将退出。

If you want the JVM to keep running, remove the setDaemon call 如果希望JVM继续运行,请除去setDaemon调用

You fired the thread without ever tell Java that you want the work done back. 您在没有通知Java的情况下就触发了该线程,您希望完成该工作。 You need to add a mnz.join() after your start ;) 您需要在开始后添加一个mnz.join() ;)

With that fix, your thread run one time for sure. 有了该修复程序,您的线程肯定会运行一次。 If you decide to put all your run() code inside of a while loop and change your loop, you got the behaviour that you want. 如果决定将所有run()代码放入while循环内并更改循环,那么您将获得所需的行为。

Like this 像这样

@Override
public void run() {
    while (true) {
        // TODO Auto-generated method stub
        try {
            while (min == 0) {
                System.out.println("Enter minutes to snooze..");
                min = scn.nextInt();
            }

            convertedToMiliSec = TimeUnit.MINUTES.toMillis(min);
            try {
                Thread.sleep(convertedToMiliSec);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            System.out.println("Alarm Now!!!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    mnz.setDaemon(true);
    mnz.start();
    mnz.join();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM