user1409534 :
Say I have thread pool and I'm executing a task from that thread pool with thread with name thread-a
Now within thread-a
I'm starting a new thread lets call it thread-child
(might be pooled thread and might not) does this means that thread-a
is going back to the thread pool when thread-child
get running? or thread-a
is going to die?
dkb :
This is the actual answer : https://stackoverflow.com/a/56766009/2987755,
Just adding code in addition to that.
ExecutorService executorService = Executors.newCachedThreadPool();
Callable parentThread = () -> {
System.out.println("In parentThread : " + Thread.currentThread().getName());
Callable childThread = () -> {
System.out.println("In childThread : " +
Thread.currentThread().getName());
Thread.sleep(5000); // just to make sure, child thread outlive parent thread
System.out.println("End of task for child thread");
return 2; //ignore, no use here
};
executorService.submit(childThread);
System.out.println("End of task for parent thread");
return 1; //ignore, no use here
};
executorService.submit(parentThread);
Thread.sleep(8000); // wait until child thread completes its execution.
executorService.shutdown();
Output:
In parentThread : pool-1-thread-1
End of task for parent thread
In childThread : pool-1-thread-2
End of task for child thread
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=328840&siteId=1