This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
I have a situation where I call several WebServices that take long time to return. Threads came to mind. In some cases though I need the returned values right after the call. Does it make any sense to make a Thread, start it and then call join() straight away, waiting for results. Are there any benefits doing it this way or is it just clutter?
I like Chris' solution if you do want to spawn the task into a different Thread. But the question remains - is there a reason to actually spawn the long-running task into a different Thread at all? If the code in your current Thread must perform 2 steps: 1) Start the long running job, 2) Wait for the long running job to finish with no work in between then there is no need to spawn the task into a different Thread.
The only reason to use Threads is to take advantage of parallelism. In this case you aren't getting anything done in parallel - it is a serial progression: Start Task - Do Task - Use Results. As long as it is one task, and it has to be sequential like you described, then I see no reason to split the Do Task part into a new Thread.
Steve
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
posted
0
Thanks for the responses. Given all this info I'll probably stick to serial progression.
I have a situation where I call several WebServices that take long time to return. Threads came to mind. In some cases though I need the returned values right after the call. Does it make any sense to make a Thread, start it and then call join() straight away, waiting for results. Are there any benefits doing it this way or is it just clutter?
Thank you.
Hi Martin,
Looking at your problem, i have same problem. As such its look like all your web services are independent of each other. What you need to do is to create array of thread objects, and start all thread in loop. So all parallel call will be initiated. And after this loop. Write another loop to iterate this array of thread and call join on every thread. So parallelism is achieved.