For example, I execute runnable block via ExecutorService instance and that runnable block execute some asynchronous code via the same ExecutorService instance. So my code looks like:
final ExecutorService eService = Executors.newFixedThreadPool(nThreads);
eService.execute(new Runnable() {
public void run() {
eService.execute(new Runnable() {
public void run() {
System.out.println("Successfully created");
}
});
}
});
- Is this code thread safe, because ExecutorService doesn't have a state?
In my real app I have some threads that created new threads inside them and I want to use one ExecutorService instance(maybe a bad practice?) with configurable thread pool size.
- Is it a bad practice to do have one ExecutorService instance for different threads?
- And if yes, maybe there are some alternatives?
ExecutorServicecertainly has state. It's just thread-safe so multiple threads (even those that are running in its own pool) can submit jobs to it.