I have a Thread pool in my Java.NIO socket server.
I sometimes received runtime errors like Connection reset by peer
or Broken Pipe
, etc.
My question is: is the thread getting killed when an exception is thrown? If it is - is a new thread created in the thread pool in place of the killed thread??
This is my ThreadManager:
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadsManager {
private ExecutorService threadPool = null;
private LiveConnectionsManager liveConnectionsManager;
private int threadPoolSize = 35; //number of tasks thread
public ThreadsManager(LiveConnectionsManager liveConnectionsManager) {
this.liveConnectionsManager = liveConnectionsManager;
threadPool = Executors.newFixedThreadPool(threadPoolSize);
ServerActions.threadPool = threadPool;
}
public void processNewMessage(SocketChannel socketChannel, Client client)
{
threadPool.execute(new MessagesProcessor(socketChannel, client, liveConnectionsManager));
}
public void closeConnection(SocketChannel socketChannel, Client client) {
threadPool.execute(new LogoutClient(socketChannel, client, null));
}
}
The setKeepAliveTime() method of ThreadPoolExecutor class sets the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.
Once a thread in the thread pool completes its task, it's returned to a queue of waiting threads. From this moment it can be reused. This reuse enables applications to avoid the cost of creating a new thread for each task.
You'll most likely just get worse performance. Keep in mind with Task and ThreadPool. QueueUserWorkItem , the threads are re-used when they're done. If you create a task or queue a threadpool thread, it may or may not create a new thread to execute your code.
A thread pool reuses previously created threads to execute current tasks and offers a solution to the problem of thread cycle overhead and resource thrashing. Since the thread is already existing when the request arrives, the delay introduced by thread creation is eliminated, making the application more responsive.
As you using ExecutorService to create thread pool.
So yes, ExecutorService.newFixedThreadPool(..) ensures that number of threads are constant if they get killed by any exception/error and there are enough task waiting for thread. Below text from java doc states it clearly :
Straight from java doc :newFixedThreadPool
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html#newFixedThreadPool(int)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With