Waiting on a Thread

J

Jason Cavett

I want to start a thread and wait for it to finish (using join(), it
seems) before I continue on in my application. The problem is, this
thread eventually opens up a "Save As..." dialog box. Well, since I
am "joining" with the EventQueueThread, the Save As dialog box cannot
be drawn.

Is there any way to work around this?



Thanks
 
L

Lothar Kimmeringer

Jason said:
I want to start a thread and wait for it to finish (using join(), it
seems) before I continue on in my application. The problem is, this
thread eventually opens up a "Save As..." dialog box. Well, since I
am "joining" with the EventQueueThread, the Save As dialog box cannot
be drawn.

Is there any way to work around this?

wait() and notify() instead or do I understand you problem wrong?
If you want to wait for the end of the Thread to avoid that the
application stops running, start the Thread as non-daemon (that's
the default anyway so you should be fine). A virtual machine don't
stop as long as there is at least one non-daemon-Thread running
(and no external signals say different ;-)


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
P

Patricia Shanahan

Jason said:
I want to start a thread and wait for it to finish (using join(), it
seems) before I continue on in my application. The problem is, this
thread eventually opens up a "Save As..." dialog box. Well, since I
am "joining" with the EventQueueThread, the Save As dialog box cannot
be drawn.

Is there any way to work around this?

Having the event handling thread wait for another thread is just as bad
as doing a big task in it.

Could you change to having the event handling thread do something when
the other thread finishes? The other thread would do an invokeLater at
the end of its work.

Patricia
 
D

derek

I want to start a thread and wait for it to finish (using join(), it
seems) before I continue on in my application. The problem is, this
thread eventually opens up a "Save As..." dialog box. Well, since I
am "joining" with the EventQueueThread, the Save As dialog box cannot
be drawn.
Is there any way to work around this?
Thanks

I would use a CountDownLatch.

http://java.sun.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html

From the documentation:

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
 
D

Daniel Pitts

Jason said:
I want to start a thread and wait for it to finish (using join(), it
seems) before I continue on in my application. The problem is, this
thread eventually opens up a "Save As..." dialog box. Well, since I
am "joining" with the EventQueueThread, the Save As dialog box cannot
be drawn.

Is there any way to work around this?



Thanks

Why are you joining on the event queue? You should never block the event
dispatch thread. If you want to do something once a "thread" is
finished, have that thread fire its own event on the event queue.

Hope this helps,
Daniel.
 
J

Jason Cavett

Having the event handling thread wait for another thread is just as bad
as doing a big task in it.

Could you change to having the event handling thread do something when
the other thread finishes? The other thread would do an invokeLater at
the end of its work.

Patricia

I guess that is true...I didn't think of it that way.

Well, what I am trying to do is this.

I have an Action that I overrode (SaveAction). It does a whole bunch
of stuff...stuff that I need in another spot that doesn't explicitly
require an Action. However, it seems like I should reuse this code
(which does exactly what I need) instead of copying and pasting the
code (which is prone to errors, etc). The problem I have is that my
Action is threaded (the stuff it does is done in its own thread). In
the code I want to use it in, though, I need to wait to make sure the
save has completed successfully before I continue.

I suppose I should not specifically thread the Action and instead have
whatever is using the Action to do the threading. Is this a better
way to think about this? I hope all my rambling made sense. It's
getting late and the code is getting fuzzy.
 
P

Patricia Shanahan

Jason Cavett wrote:
....
Well, what I am trying to do is this.

I have an Action that I overrode (SaveAction). It does a whole bunch
of stuff...stuff that I need in another spot that doesn't explicitly
require an Action. However, it seems like I should reuse this code
(which does exactly what I need) instead of copying and pasting the
code (which is prone to errors, etc). The problem I have is that my
Action is threaded (the stuff it does is done in its own thread). In
the code I want to use it in, though, I need to wait to make sure the
save has completed successfully before I continue.

I suppose I should not specifically thread the Action and instead have
whatever is using the Action to do the threading. Is this a better
way to think about this? I hope all my rambling made sense. It's
getting late and the code is getting fuzzy.

Could you take the whole "stuff", and run it in another thread? If it
needs to be done frequently, you could have a stuff-running thread that
gets its orders e.g. from a queue.

Patricia
 
J

Jason Cavett

Jason Cavett wrote:

...






Could you take the whole "stuff", and run it in another thread? If it
needs to be done frequently, you could have a stuff-running thread that
gets its orders e.g. from a queue.

Patricia

No, the stuff (saving in this case) doesn't run often enough to really
need a stuff-running thread (loving the technical terms, BTW).

It might make more sense to just let the developer handle threading if
it is necessary and the actions themselves are not threaded.
 
P

Patricia Shanahan

Jason said:
No, the stuff (saving in this case) doesn't run often enough to really
need a stuff-running thread (loving the technical terms, BTW).

If it needs to be run infrequently, you can just create a thread to do
it, including waiting for the other thread, each time.
It might make more sense to just let the developer handle threading if
it is necessary and the actions themselves are not threaded.

Well, be careful about what you try to do in the event handling thread.
It should generally be a sort of manager, doing the minimum to control
the display and assigning any other non-trivial work to other threads.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top