• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling join() right after start()

 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey ranchers!

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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if your application code requires the values retured by webservices to proceed.then it will make sense that you will use join() method.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a use case for FutureTask ... http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/FutureTask.html rather than join as you want a value back, several advantages over join ...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the responses. Given all this info I'll probably stick to serial progression.
 
Narendra shah
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vanyavchich wrote:Hey ranchers!

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.

 
Sandeep Sanaboyina
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use Callable instead of Runnable. You can start multiple threads and they will wait for the value to be returned.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!