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.
source share