I had my original threading code which worked well, but since my tasks were shortlived, I decided to use thread pools through ExecutorService
.
This was my original code
public class MyRun implements Runnable
{
private Socket socket = null;
public MyRun(Socket s)
{
socket = s;
thread = new Thread(this, "SocketThread");
thread.start();
}
public void run()
{
// My actual thread code
}
}
My main program
...
ss = new ServerSocket(port);
....
MyRun st = null;
while (!stop)
{
st = new MyRun(ss.accept());
st = null;
}
New code
public MyRun(Socket s)
{
socket = s;
thread = new Thread(this, "SocketThread");
}
run()
left unchanged
Changed Main program
private static ExecutorService execService = Executors.newCachedThreadPool();
....
....
while (!stop)
{
execService.execute(new MyRun(ss.accept()));
}
Changed code seems to be working fine, but I just want to make sure there is nothing I am missing. I want all threads to execute simultaneously.
2 Answers 2
A few simple remarks :
thread = new Thread(this, "SocketThread");
is no longer needed in MyRun, since the ExecutorService is the one creating and managing theThread
s.- you will want to call
execService.shutDown()
to properly clean up the resources of the executorService.
As I understand, you're still using blocking I/O. So each connection still consumes the whole thread, and there is little difference whether this thread was created manually or taken from a pool.
To employ thread pool efficiently, you have to use non-blocking I/O (NIO), but it is harder to use.
So the question is, have you enough memory to spend a thread per connection? If yes, continue to use your old code, executor service wouldn't help you. If no, take a NIO library (Netty is most widely known, df4j - almost unknown), and follow its suggestion how to use executor service.
-
\$\begingroup\$ Couple of things - Is NIO really faster - mailinator.blogspot.in/2008/02/…. Also, won't using a thread pool be advantageous in terms of the cost of creating a new thread - i.e. each thread runs for a very short time in my case. So may be cost of creating the thread is high as compared to the length of time the thread runs. \$\endgroup\$user93353– user933532013年05月03日 13:48:17 +00:00Commented May 3, 2013 at 13:48
-
\$\begingroup\$ The OP was concerned about creating a new thread for each request--not about using too many concurrent threads. A thread pool alleviates this problem. NIO would be helpful if the volume of requests is consuming too many resources. \$\endgroup\$David Harkness– David Harkness2013年05月03日 18:20:00 +00:00Commented May 3, 2013 at 18:20