2

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.

Dimitar
  • 4,402
  • 4
  • 31
  • 47
Rohit Kasat
  • 300
  • 3
  • 13

3 Answers3

1

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.

See also: Invoking a java Timer-Task from the timer thread

Community
  • 1
  • 1
Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
1

So my question is can jvm abruptly kill any thread.

Generally, it depends on the thread and how it is created and what you mean with JVM abruptly kill. The JVM informs all daemons when it shuts down. Please have a look at:

what is the best solution to schedule a task . since my task execution time is coming at runtime I cant use fixed schedule executors.

In your case, I think that the best solution is to use the implementations provided. Why can't you use a ScheduledExecutorService, I don't understand the runtime argument. Have a look at Stefan Dollase answer, because he gives some more insights regarding it's usage.

Community
  • 1
  • 1
Philipp
  • 1,289
  • 1
  • 16
  • 37
  • so I cant use ScheduleExecutor because the schedule I get is coming from scheduled thread. so Firsts thread execution will give me schedule for second execution. Abruptly meaning If there is a out of mememory or any other kind of error in jvm, will it cleanup threads? and can kill my thread or try to restart it? – Rohit Kasat Mar 24 '16 at 00:35
  • 1
    It depends on how the JVM shuts down. In general, the JVM tries to do a graceful shutdown for all your threads. But it only shuts down if there aren't any non-daemon threads running. If you have an exception in a thread, the thread exits, like any other thread. Have a look at http://stackoverflow.com/questions/6546193/how-to-catch-an-exception-from-a-thread if you want to know how to catch exceptions in threads. – Philipp Mar 24 '16 at 00:39
0

See if it is a daemon thread, it will not be stopped until:

  1. Job given to thread is finished.
  2. System.exit() is called.
  3. If an error or exception occurs.
  4. If the JVM is stopped.

Here only the 3rd problem concerns as. To resolve this problem use try-catch which catches Throwable(This will catch everything), and in the catch you write a mechanism to re-run the function.

padippist
  • 1,178
  • 1
  • 16
  • 30