5
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

6
\$\begingroup\$

A few simple remarks :

  • thread = new Thread(this, "SocketThread"); is no longer needed in MyRun, since the ExecutorService is the one creating and managing the Threads.
  • you will want to call execService.shutDown() to properly clean up the resources of the executorService.
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\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
    Commented 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\$ Commented May 3, 2013 at 18:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.