IsaacLevon
IsaacLevon

Reputation: 2570

readTimeout in a multithreaded environment

I have a system consists of workers that crawl web pages. Now, I set two parameters of URLConnection: connectTimeout and readTimeout.

As inferred, the app is multithreaded.
Questions:

  1. It means that if I set the timeout to, let's say, 10 seconds. These aren't really 10 seconds the thread tried to read. Practically, if we have many threads it could be that our threads was starved and by the time it get a chance to keep (or even start) reading it's getting a timeout
  2. If the above is true, how can I deal with this problem? The first things that comes to mind is just choosing the right numbers, taking into account the number of threads.

Is there a better solution?

Upvotes: 0

Views: 57

Answers (1)

Michael Gantman
Michael Gantman

Reputation: 7790

Simply put, unless you use the number of threads in the 100s then chances are you don't have to worry about starvation. The time that a thread loses due to context switching is really negligible. (Remember your computer most likely has several processors and also IO may be processed IO processing component and not a processor). So in most cases your 10 seconds timeout is really a 10 seconds timeout.

Upvotes: 1

Related Questions