Summary
I have a server that should be long-running, and that spawns a few background threads for IO. I'm trying to make sure that the background/IO threads don't go down, or that they'll be brought back up if they do go down.
Current Solution
Currently my main loop just checks the status of all background checks (pseudo-code below). I think there should be a better way.
while (!Thread.currentThread().isInterrupted()) {
maintainThreads();
doWork();
condition.await(30, TimeUnit.SECONDS);
}
My Attempt
I'm considering switching to a SingleThreadExecutor
, with a custom queue
that won't remove the Runnable
when it pulls the next task. The executor
would then manage the threads for me so I could take it out of my main loop.
I'm worried that having one executor for each thread will be a performance hit, and that there are simpler/better solutions that exist for this problem. I've also considered setting up shutdown hooks for each thread to have them just restart themselves.
Any help would be appreciated.