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
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