Event after thread termination

M

Mike Mimic

Hi!

How can I make an event handler in main thread which would be called
after another thread terminate?


Mike
 
C

Chris Smith

Mike said:
How can I make an event handler in main thread which would be called
after another thread terminate?

You can't ever cause some method to be executed asynchronous within a
specific other thread, without instrumenting the target thread to do so.
So your main thread would need to watch for this to happen on occasion,
and execute your event handler when it does. You'd need to arrange for
that polling to occur on a regular basis. It could do this polling
using the isAlive() method of its target thread.

This all raises the question of why it's so important for you to have
the event handler called *in the main thread*. It would be far easier
to provide for such a notification run in another thread (depending on
your exact requirements, either the target thread that's about to
terminate, or a third watchdog thread).

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mike Mimic

Hi!
You can't ever cause some method to be executed asynchronous within a
specific other thread, without instrumenting the target thread to do so.
So your main thread would need to watch for this to happen on occasion,
and execute your event handler when it does. You'd need to arrange for
that polling to occur on a regular basis. It could do this polling
using the isAlive() method of its target thread.

This all raises the question of why it's so important for you to have
the event handler called *in the main thread*. It would be far easier
to provide for such a notification run in another thread (depending on
your exact requirements, either the target thread that's about to
terminate, or a third watchdog thread).

What do you think about this solution.

At the end of run method of the thread for which I want event after
termination I add:

SwingUtilities.invokeLater(new Runnable() {
public void run() {
((MyMainWindowClass)owner).onTerminate();
}
});

where owner is a reference to MyMainWindowClass object (I pass it with
constructor). onTerminate is method in MyMainWindowClass.


Mike
 
X

xarax

Mike Mimic said:
Hi!


What do you think about this solution.

At the end of run method of the thread for which I want event after
termination I add:

SwingUtilities.invokeLater(new Runnable() {
public void run() {
((MyMainWindowClass)owner).onTerminate();
}
});

where owner is a reference to MyMainWindowClass object (I pass it with
constructor). onTerminate is method in MyMainWindowClass.

That technique will run the Runnable object on the AWT
Event Dispatch Thread.

From your posts, you seem to not understand the
differences betweem a thread, a class, and an
instance of a class.

Go back to Java 101 and start over.
 
M

Mike Mimic

Hi!
That technique will run the Runnable object on the AWT
Event Dispatch Thread.

And is not this exactly what I need? On which thread
does all clicks and other events (events generated
by window) happen? Is not that AWT Event Dispatch Thread?
Are there any other threads in simple Java Swing program
(except for thread which calls main)?

So after calling main and after that when main shows window
and exits itself. Is not AWT Event Dispatch Thread that
where methods called by events occur?
From your posts, you seem to not understand the
differences betweem a thread, a class, and an
instance of a class.

Can you point me to those mistakes? I think that I not
only know the difference but also what a thread, a class and
an instance of a class are. But I would be glad to learn
my mistakes.


Mike
 
C

Chris Smith

Mike said:
And is not this exactly what I need? On which thread
does all clicks and other events (events generated
by window) happen? Is not that AWT Event Dispatch Thread?
Are there any other threads in simple Java Swing program
(except for thread which calls main)?

You've got it right. I think "xarax" was confused that you said you
wanted a method called in the main thread. At least that confused me at
first. In Java, the "main thread" normally means the thread that calls
main, and of course that's a different thread from the AWT event
dispatch thread.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top