aspose file tools*
The moose likes Threads and Synchronization and the fly likes problem stopping GUI thread in run(). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "problem stopping GUI thread in run()." Watch "problem stopping GUI thread in run()." New topic
Author

problem stopping GUI thread in run().

Rick Stewart
Greenhorn

Joined: Mar 02, 2006
Posts: 16
Hello, I hope this is not one of those " Oh My God, EVERYBODY knows that!" kind of questions. But implementing threads is new for me.

I have a GUI application that is allowed to spawn new copies of itself, which then in turn can spawn more new copies, etc.. No problem there, that functionality works. My problem comes when trying to shut them down. Each spawned GUI application must be able to terminate itself without terminating any of the other copies, "killSelf". They must each also be able to terminate all running GUI applications, "killAll". The killAll part is easy, any GUI copy calling System.exit() stops the JVM, so they all will stop.

My question is, how do I implement "killSelf"? I had thought that when a thread is started inside run(), and then when run() finishes that thread should terminate. But in the code below, when run() returns and the message "exiting run()" is printed to the console, the new MyGUI application is still running. How can I get it to terminate?

The commented out code was going to be how I stopped the new MyGUI, but it didn't work.

Here is a sketch of my code:



Any suggestions whould be appreciated. Perhaps there is a pattern I should be following?

Thanks,

Rick Stewart

[ September 30, 2006: Message edited by: Rick Stewart ]

[ September 30, 2006: Message edited by: Rick Stewart ]

[ September 30, 2006: Message edited by: Rick Stewart ]
[ September 30, 2006: Message edited by: Rick Stewart ]

Honesty IS the best policy, but occasionally reality throws an exception...
Rick Stewart
Greenhorn

Joined: Mar 02, 2006
Posts: 16
Sorry the code looks like heck, I couldn't get tab to work, and when I posted the message, all the leading line spaces that were formatting the code were removed.

Rick...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

We've got these "[ CODE][ /CODE]" tags you can use to preserve formatting in code; see the "CODE" button below the text entry box.

Now, as to your problem: I see a whole bunch of different places where startGUI is going to be called, which doesn't seem right; you want to build a window just once, right? But let's assume you have a handle on that.

If you build a Frame in a Thread, then that Frame will outlive that Thread. There's no special relationship between the Thread and the Frame; when the Thread dies, the GUI remains. Since you don't show a lot of the critical code, I can't say for sure, but the Thread probably exits as soon as the Frame is all put together, anyway.

To dispose of a Frame, call (wait for it...) the dispose() method of the Frame. If your Frame has some extra cleanup to do, then override dispose(), add the extra code, and then call super.dispose().


[Jess in Action][AskingGoodQuestions]
Rick Stewart
Greenhorn

Joined: Mar 02, 2006
Posts: 16
Hello Ernest, thanks for your reply, and the tip about using , that sure makes everything look better!

There are just 2 places where startGUI() gets called, the first is in main() when I first run "c:\>java MyGUI", and then thereafter its only ever called from inside run() when I start up a new copy.

As you suggested I've looked up Window dispose(), and it is just the thing I was looking for I think. I have lots of books that explain threads and run(), but all the examples dealt with simple threads, and none that I saw dealt with a thread that would live past the life of the run() method.

Thanks very much,

Rick Stewart
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Rick,

Note that the thread does not live past the end of the run() method. The statements in the run() method execute, and then the thread exits. But its side effects -- such as creating some GUI objects -- live on. Imagine a Thread that calls System.out.println() in its run method. When the thread terminates, you don't expect the messages to somehow become "unprinted", do you? Same thing.
Sandeep Kumar S Jakkaraju
Ranch Hand

Joined: Oct 18, 2006
Posts: 37
try writing a stop method ...


synchronized void stop()
{
spawn=null;
}


I write like Jean Paul Sarte but in Java....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem stopping GUI thread in run().