I have a class (let�s call it Sender) that runs through a variety of data objects, spawning threads to process each one.
I created a class that implements Runnable and, of course, implements the run() method. When it is instantiated, a reference to Sender is passed to it. At the end of run(), it calls a callback method in Sender, giving it identifying information so Sender knows that data object has been processed and can delete it from the database table listing the data objects to process.
Sender creates an ExecutorService using Executors.newFixedThreadPool(). The number is set in a properties file, useful for debugging purposes and for fine-tuning performance and memory issues.
My Runnable objects are instantiated and given to the ExecutorService using the execute() method. After all data objects have been sent, Sender calls the shutdown() method.
All nice and, as far as I can tell, it should work beautifully. But there�s only one problem. The threads (which involve time-consuming tasks and actually spawn child processes- see the thread at http://www.coderanch.com/t/234068/threads/java/Limit-concurrent-threads-report-successful for more background) never finish processing! However, when I have Sender sleep after executing all the threads, some complete. With enough time- or if I have sender loop, sleeping until threadExecutor.isTerminated()- then all the threads successfully complete processing.
I do not set the threads to be daemon threads. I do not believe the ExecutorService creates them at daemon threads. The main process should continue running until all threads are complete, correct? Any ideas why it�s stopping, and taking the threads with it?
Of course, if I don�t call shutdown() the process NEVER stops, as the ExecutorService waits indefinitely for more tasks to execute.