I am planning to create a thread which will behave like a timer. I will use sleep to retrigger the operation after specific delay..
I am worried if JVM can abruptly kill my thread without closing the application,
So if app is running and this thread dies my feature won't be able to get a fresh token.
Either I will have to write some manual feature to restart it.
So my question is: Can JVM abruptly kill any thread?
What is the best solution to schedule a task? Since my task execution time is coming at runtime I can't use fixed schedule executors.
To my knowledge, the JVM will not randomly kill any thread, since this would result in undefined behavior for all Java programs. However, the JVM will shutdown itself and kill any running thread which is marked as daemon thread if there is no running thread which is not a daemon thread.
Quoting from Thread::setDaemon
:
Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.
To repeatedly schedule a task you should use an ScheduledExecutorService
whenever possible. The fact that you get the delay at runtime does not prevent you from doing so.