So, if I understand your question correctly, you want each 'work thread' T1..T3 to have its own background thread (dt1…dt3) doing some co-processing, and you want the background thread to exit when your main thread exits, yes? You could do something like this:
Make each 'main thread T1… a Runnable that looks like this, so that when you launch your T1, it launches its own dt1, and then asks it to shutdown (via interrupt()) when it finishes.
@Override
public void run() {
ExecutorService e = Executors.newSingleThreadExecutor();
Runnable r = new Runnable() {
@Override
public void run() {
// this is your deamon thread
boolean done = false;
while (!done && !Thread.currentThread().isInterrupted()){
// Do your background deamon stuff here.
//
// Try not to use blocking, uninterruptible methods.
//
/* If you call a method that throws an interrupted exception,
* you need to catch that exception and ignore it (or set done true,)
* so the loop will terminate. If you have other exit conditions,
* just set done=true in this body */
}
}
};
e.execute(r); // launch your daemon thread
try {
// do your main stuff here
}
finally {
e.shutdownNow(); // this will 'interrupt' your daemon thread when run() exits.
}
}