Create a daemon as a thread in java, which can only be killed at certain safe points?

The code I inherited is a server that generates many different types of daemon threads that receive and respond to requests as they arrive. Obviously, this is dangerous and needs to be retrained. As now, if the main program is stopped, and one of the daemons serves the request, the thread can be killed halfway through the request and leave something in an inconsistent state.

However, there are quite a few threads in different areas of the code. If I had to manually close each of the threads when I called shutdown, then it can be a little painful to achieve the perfect logical thread without missing some obscure daemon.

Instead, I would like to have a stream similar to a daemon stream, but I can mark or switch part of the stream section as a critical section; who will complete therad from being reaped until he completes. While the daemon blocks and waits for requests that behave like a daemon thread, it does not prevent the virtual machine from closing and immediately stops if the VM shuts down. However, although the thread is actively serving a specific request (part of the time when the thread is active), the thread will not be killed until it terminates and leaves its critical section. As soon as the thread finishes the critical section, it becomes suitable for killing. Ideally, when there are no more non-daemon threads, the virtual machine will start the shutdown process immediately, even if some daemons are still doing critical work, receiving any daemon not in critical condition, and then waiting for each remaining daemon to exit, this is a critical point so that she could be killed.

Is there an easy way to get this behavior by simply creating an instance of the Thread class (maybe I'm writing) or setting a boolean instead of explicitly writing each thread to handle interrupts correctly to behave like this? I am mainly looking for an idiotic way so that if the plugins working in such a thread are not being written to handle interrupts perfectly, the thread will still correctly fill its critical section and then exit when the VM shuts down.

+4
source share
3 answers

However, there are quite a few threads in different areas of the code. If I had to manually close each of the threads when I called shutdown, then it can be a little painful to achieve the perfect logical thread without missing some obscure daemon.

Unfortunately, the best way to do this is as you imply. You must have a destroy() method for classes that use fork threads so that they can explicitly clear themselves. But it requires someone to call these destruction methods when the application ends.

Instead, I would like to have a thread that looks like a daemon thread, but has a certain critical section where it cannot be killed until it completes (or maybe it does not pass if it takes too long?) .

Nothing is allowed in Java threads. Either the thread is a daemon, or it is not there, and it is installed before the start of the flow.

Is there an easy way to get this behavior by simply instantiating the class or setting the boolean

I think you are here. I would have a ThreadUtils class with a volatile boolean shutdown field.

  public class ThreadUtils { private static volatile boolean shutdown = false; /** called by main when the application is shutting down */ public static void shutdown() { shutdown = true; } /** used by the various non-daemon threads to test for shutdown */ public static boolean isShutdown() { return shutdown; } } 

The main program will set the completion flag to true, and then all your threads will need to check this boolean in their code:

  // we can test for shutdown only at "appropriate" points in the thread while (!ThreadUtils.isShutdown()) { ... // we are not ready to be killed here ... } 

Something like this template, although a little crude, seems to fit your requirements.

+5
source

I am trying to improve @Gray's latest answer.

You can maintain a counter of currently running threads. Each new start of a thread will increase this counter, and each thread that completes run() will decrease it. You can then add the shutdown hook to your main thread using the Runtime.addShutdownHook() API. When the java process signals completion, this hook will set the condition for the global flag ThreadUtils.isShutdown() to true and wait until all started threads complete and naturally disappear when all threads complete.

To prevent undefined waiting, you can use the timeout inside the run() method. If a timeout occurs, run() will terminate immediately, and all daemon threads will also be completed.

+1
source

So, all of these daemon threads are requests for handling event loops. What if you just send a shutdown request for all of them so that they can work together. Each thread probably has a special way to request a poll from somewhere, so you need to reorganize it a bit.

0
source

All Articles