• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

calling join() right after start()

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the responses. Given all this info I'll probably stick to serial progression.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic