End suicide on shutdown? stop java.util.Timer instance

I have java.util.Timer working with fixed interval. I added Runtime#addShutdownHook and shuts down when the virtual machine finishes normally or abnormally. However, it keeps the virtual machine alive when main completes, unless I insist on running System.exit on main . Is there a way to check if I am the last character of Thread , or in some other way to avoid changing the main , which will complete normally with completion?

<sub> Note: I know that many people think java.util.Timer deprecated (it’s not), but if your alternative doesn’t help me solve this problem ... Sub>

+4
source share
1 answer

Create a timer using one of the constructor overloads, which allows you to specify whether to use the daemon thread: pass true as an argument so that it runs in the daemon thread, which will not prevent the virtual machine from closing.

+8
source

Source: https://habr.com/ru/post/1309000/


All Articles