Hi,
In my code, I call a thread
and then just below the call to the thread there are some other command
which I need to execute just after the call to the thread finishes,...
But now because I'm using a thread, the batch of command gets executed
first...
Is there any way I can do to get the batch executed after the thread???
Thanks
Patricia Shanahan - 25 Aug 2005 04:53 GMT
> Hi,
> In my code, I call a thread
[quoted text clipped - 5 lines]
>
> Thanks
Either:
Don't construct the thread at all. Just call the run() method for the
Runnable on which it is based. This seems to me to be the cleaner
approach. Why mess with an extra thread if you don't want any parallelism?
or:
Call the thread's join() method.
Patricia
omurice - 25 Aug 2005 05:32 GMT
I can't because I'm writing for a mobile application device and
to avoid deadlock, operations that may block such as networking should
be performed in different thread,..hence I should construct the thread
Any thoughts?
Gordon Beaton - 25 Aug 2005 07:53 GMT
> I can't because I'm writing for a mobile application device and
> to avoid deadlock, operations that may block such as networking should
> be performed in different thread,..hence I should construct the thread
But since you wait for the thread, you have not reduced that risk.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Patricia Shanahan - 25 Aug 2005 08:27 GMT
>>I can't because I'm writing for a mobile application device and
>>to avoid deadlock, operations that may block such as networking should
[quoted text clipped - 3 lines]
>
> /gordon
Indeed. The design for what happens in which thread needs to be thought
through, taking into account the rules about what can wait for what.
Let's give the entities involved some names:
As I understand the problem, thread A spawns thread B to run task X, and
then itself does task Y. Task Y must not start until task X completes.
Because of other responsibilities, thread A cannot wait for task X to
complete.
In that case, thread A cannot be the thread that runs task Y.
The easiest way to ensure that Y does not start until X finishes is
still to have one thread do both of them, in the proper order. How about
having thread B do task X followed by task Y?
Patricia
Roedy Green - 29 Aug 2005 23:57 GMT
>In my code, I call a thread
>and then just below the call to the thread there are some other command
>which I need to execute just after the call to the thread finishes,...
>But now because I'm using a thread, the batch of command gets executed
>first...
>Is there any way I can do to get the batch executed after the thread???
If the last thing you do in your run method is an exec or a
ProcessBuilder.start then the spawn won't happen until everything else
is done.
If the first thing you do in your run method is an exec or a
ProcessBuilder.start then the spawned process will execute in parallel
with your thread code, unless you program in a wait.
See http://mindprod.com/jgloss/exec.html
Java code is always more eloquent than English. Post code if you want
people to unambiguously understand what you are asking.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.