views:

109

answers:

1

I am trying to optimize the code for an application we are currently developping. The current implementation invovles creating a thread to continously poll a server for new data. If new data is found the thread then spawns multiple threads (created every time) to retrieve the data.

I was reading regarding thread pooling and was curious wether changing the code to utilize the threadpooling would result in noticeable difference.

+6  A: 

If you've created a single long-running thread to do the polling, you wouldn't notice much difference. Admittedly you probably have a sleep at the moment, so you've got one more thread than you need for a lot of the time (compared with a timer-based solution) but it's basically okay.

Thread pools are particularly good when you have a lot of short-lived operations. Starting a thread doesn't come for free, and the overhead is pretty large if the actual work is trivial. Thread pools just keep the actual thread hanging around, waiting for work item. They can then execute those work items with very little overhead.

With a single long-running task, the overhead is insignificant compared with the time spent in the task itself, so you wouldn't notice any difference.

Jon Skeet