views:

141

answers:

2

Hello developers!

I have a Java program that uses reflection to find and invoke the main(String[] args) method of another Java program. This other program is usually a Swing App that tends to spawn its own threads. I was wondering if there was a way to determine when this other 'program' terminates. I'm not sure if there is anyway to detect this since it executes in the same space as the host program. The current method is just to check if any of the frames open aren't our own, not exactly the greatest solution. Running the child program in its process also isn't really an option since we access to it.

EDIT: It looks the answer is a dual approach. First create a ThreadGroup and launch the child program in a thread that is a member of that group. Then make sure to check Frame.getFrames() if any of the frames belong to child.

Thank you, --Sandro

+1  A: 

Start the child app in a separate ThreadGroup (probably best set it up as a daemon thread group) and periodically check whether it contains any running threads or subgroups.

Michael Borgwardt
Don't know. It wouldn't get a separate EDT and when the main() just quits this check might return the wrong answer I think.
kd304
You'd need to use the non-public APIs to set up an AppContext.
Tom Hawtin - tackline
trying it out now. I'll be back with results soon.
Sandro
Yes, it wouldn't get a separate EDT, but I don't see that as a problem; either it uses the existing EDT from the parent app, or its EDT dies as a daemon thread left in a daemon thread group. Of course, if the child app calls System.exit(), you have a problem, but that's a different matter.
Michael Borgwardt
Fair explanation.
kd304
Ok so now for the follow up question. What's the best way to check if a thread group is done? threadGroup.enumerate(threads, true); keeps returning 0 but there is a frame open. Should I both check for frames and the ThreadGroup?
Sandro
I guess that will happen when the child app doesn't start any threads of its own and only opens a GUI, waiting for user actions. In that case, I suppose you'll also have to check open frames.
Michael Borgwardt
The real problem is that the property "child app has terminated" is not really well-defined, since a Swing GUI doesn't really DO anything except sitting there and waiting for user input.
Michael Borgwardt
A: 

If you have ways to discover the windows this child application is using you could just attach the appropriate Window listeners and wait for all the close event.

Edit:

Using the event listener approach at least allows you to not loop and query constantly for the frames. For the case of the spawned threads I have no clue. If there is a way you could track your own threads and discover the app's threads you could do the same looping as for the Frames.

Edit 2:

What comes into my mind is to use JMX on yourself to get the threads and other information, but I don't know JMX in much detail.

kd304
But what if it spawns threads? How do I keep track of those? And right now I'm using Frames.getFrames() to check for the windows. But regardless adding listeners to the windows is a start.
Sandro