I have a spring context in which a we have Runnable beans started like so:
public void startChildAndWait(Class type) {
BaseMyDispatcher child = appContext.getBean(type);
child.initialize(this); //The child references its parent during run method
new Thread(child).start();
synchronized(LOCK_OBJECT) {
LOCK_OBJECT.wait(someTime);
}
}
The BaseMyDispatcher class is an abstract class and SampleRunnableX are implementations which are with prototype scope, the base class basically has @PostConstruct method and a @PreDestroy method (the main functionality of which is to call notify on a LOCK_OBJECT) and of course a Run method
My problem is that the PostConstruct method is called but when the Run method completes the object doesn't seem to be destroyed therefore the PreDestroy method is not called and I get stuck waiting in parent on the LOCK_OBJECT
The code is called in a function inside a parent Runnable (which is executed inside a ThreadPoolExecutor and starts (sequentially) several children with the same method startChildAndWait passing each time a different class:
startChildAndWait(SampleRunnable1.class);
if(run2IsRequired && lastExitCode == 100) {//runIsRequired are booleans
startChildAndWait(SampleRunnable2.class);
}
if(run3IsRequired && lastExitCode == 100) {//lastExitCode is an integer
startChildAndWait(SampleRunnable3.class);
}
So what do I do to make the PreDestroy method called upon completion of the child thread?