need help with concurrent.ExecutorService

U

user00

I have a server application where clients connect directly and send
many commands to the server to process (typical server/client model). I
have something working but Im trying to be efficient as possible at the
same time. Im using ExecutorService for the thread pool and for each
command that comes in, I create a new Runnable class (to process the
command) and then I execute the Runnable against ExecutorService. I
dont know how efficient it is to create a Runnable class for each
command that is received. Here is some sample code....


ExecutorService es = Executors.newFixedThreadPool(25);

void commandRecieved(String cmd) {
es.execute(new ProcessTask(cmd));
}


//Runnable class to process the command

class ProcessTask implements Runnable {

String cmd;

public MyTask(String cmd) {
this.cmd = cmd;
}

public run() {
MyServer.process(cmd);
}

}



Is there a way around this? The task to do is the same but only
different in one parameter (the actual cmd string).

Since threads are fixed (persistent), I wonder if there is a way I can
associate a fixed Runnable task to each Thread and when I es.execute I
can actually just pass the cmd string only. I hope I am making sense?

Then again, maybe there is a better approach for my situation instead
of using ExecutorService? Maybe ExecutorService was intended for use
when you know there are going to be different types of tasks that is
going to be executed. Any help would be most appreciated.


Thanks in advance
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top