Associate
- Joined
- 14 Apr 2003
- Posts
- 1,101
Hi,
I have an application that sends numerous requests to a server. I want to speed this up by using threads. My current idea is move the code that is responsible for sending the request to a new 'Runnable' class and then have a loop to create and start this thread. Now my question is this:
What is the best way to create (and maintain) a number, say 5, threads at all times? That is, I want to have 5 threads running at all times until all requests have been sent. Should I just use a while loop with a nested if?
MyThread is then responsible for increasing the numSent variable so the while loop eventually exits. This doesn't seem very efficient to me as the main loop will be running excessively, doing nothing, while it waits for the requests to finish.
I have been looking into the wait() and notify() methods but can't seem to figure out how to apply them in this context. This must be a common design pattern?
Cheers.
I have an application that sends numerous requests to a server. I want to speed this up by using threads. My current idea is move the code that is responsible for sending the request to a new 'Runnable' class and then have a loop to create and start this thread. Now my question is this:
What is the best way to create (and maintain) a number, say 5, threads at all times? That is, I want to have 5 threads running at all times until all requests have been sent. Should I just use a while loop with a nested if?
Code:
while(numSent < totalRequests)
{
if(numThreads < maxThreads) // spare thread, create and run one.
{
MyThread t = new MyThread(<some variables>);
t.start();
}
}
MyThread is then responsible for increasing the numSent variable so the while loop eventually exits. This doesn't seem very efficient to me as the main loop will be running excessively, doing nothing, while it waits for the requests to finish.
I have been looking into the wait() and notify() methods but can't seem to figure out how to apply them in this context. This must be a common design pattern?
Cheers.