File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes calling join() right after start() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "calling join() right after start()" Watch "calling join() right after start()" New topic
Author

calling join() right after start()

Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 239
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.


SCJP 6
I no good English.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1044


if your application code requires the values retured by webservices to proceed.then it will make sense that you will use join() method.


SCJP6.0,My blog Ranchers from Delhi
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 329

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 ...


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 1811

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: 239
Thanks for the responses. Given all this info I'll probably stick to serial progression.
Narendra shah
Ranch Hand

Joined: Feb 28, 2007
Posts: 49
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.


Narendra Shah -- SCJP 5
Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 71
Why don't you use Callable instead of Runnable. You can start multiple threads and they will wait for the value to be returned.


They say you have to be the first, the best or different. I say, is it too much to ask for all three.
 
jQuery in Action, 2nd edition
 
subject: calling join() right after start()
 
MyEclipse, The Clear Choice