java thread pool
Posted: 29 March 2010 08:46 AM     [ Ignore ]  
I've Only Posted Once!
Total Posts:  9
Joined  2010-03-29

I’m working on a multithreaded echo server in java using the networking API.

I’ve got a problem with threading however. The control class is pretty simple
Code:

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class control extends Thread implements Runnable{

public static void main(String args[] ) throws IOException
{
ExecutorService execSvc = Executors.newCachedThreadPool();

for( int i = 1; i < 7; i++ )
{
execSvc.execute( new EchoServer(i) );

}
     
      execSvc.shutdown();
       
      while(true){}
}

}

As expected this runs 6 concurrent echo servers at the same time. What I’d like is to be able to create 2 concurrent servers at first and then if a client connects then another server is spawned.

I can write a small method in the EchoServer class to return whether it is connected or not but that means I need to name each thread in order to address it (EchoServer server_ID = new Echoserver(threadID)); Followed by an if statement with server_ID.isConnected(); or something similar).

Problem is I can’t figure out how to make the threads addressable. If that makes sense?

Anyone got any suggestions?

Profile