Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Master Vs Slave (MainMethods)

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this problems been driving me nuts�
How to terminate a sub �main method� class, that was initiated by another, in this case Master �main method� class�
Best explained via example�
--------------------------------------
Say I have two pre-made Java programs that I felt could work nicely together...
1) A simple �Text Editor�
2) A �Dictionary Viewer�
Both written completely separately with their own main methods (you can start either independently), so the �Dictionary Viewer� class is a kind of module program, that can be started from the �Text Editor�, which is simply done via�
Dictionary foobar = new Dictionary();
Now say after one minute, I wont to automatically close down (terminate) the �Dictionary Viewer�, setting up a timer is easy, but what boggles me is how to kill the main method class �Dictionary ( )� which I initialized in my �Text Editor� ?

Note: I know of many solutions that require merging the two codes (or similar), but I need them to be as they are both main methods that can be run independently, but with one being able to initialize the other then shut it down.
Thanks,
eung e'ustov
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your editor class, just create a new thread, with a runnable that instantiates your other application main class, and calls main on it. you can keep a reference to that class in a static variable if you like. Then you just have to implement a method in your dictionary application that can accept a quit request, and notify any threads your dictionary application spawns that they should quit.
OR, if you don't need to quit the dictionary program until the editor program quits, an even better solution is to make the thread which calls the main() method a daemon thread. When you quit the editor, the dictionary will quit too (assuming it doesn't spawn any non-daemon threads).
 
Stoic Eustov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've actually implemented the �Dictionary Viewer� as a new runnable thread, but what id like is to close the �Dictionary Viewer� from the �Text Editor�.
Implementing a 'quit request' is easy, but once the �Dictionary Viewer� kills all its spawned threads, how does it kill it self and shut down. Thanks
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming your dictionary viewer shuts down all its threads, and closes all its windows, and releases any external resources used, then all you need to do is have it exit its run method. That will end its startup/main thread. Then you just set your reference to the DictionaryView in your editor to null, and everything in the dictionary viewer is now stopped and eligible for GC.
I'm curious, what does the main() of your DictionaryViewer do?
[ February 28, 2002: Message edited by: Rob Ross ]
 
Stoic Eustov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main() of my DictionaryViewer, just opens a GUI that displays a simple dictionary, that id like to close after a period.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So as long as you close (dispose()) the GUI window, and set the reference to the DictionaryViwer() to null, you should achieve what you want.
 
Stoic Eustov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe another way of putting it, how do you get any Java main method class, to self terminate itself...
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on what you're doing in your main(). If you're running an endless loop, you just need some way to flip a boolean flag to let the loop know it can end, or break out of the loop, etc.
If your main() is just constructing a Frame, it will exit afterwards. So main() in this case exits even though your program still runs. Here's an example of this:

When you run it, it first prints out a list of all the currently running threads, then instantiates a new Frame2 object. After this, main will exit. Meanwhile, your Frame is displayed and events are handled in various other Threads. You can click the button to verify that the main() thread is no longer running - it's ended.
So you can see you don't really have to do anything special to end main() in this program.
 
Stoic Eustov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry what i meant was how do you get a Java class (that is a main method), to self terminate itself, when spawned from another class...
Because using System.exit(); will kill both as thet both run on the same JVM session...
<CODE><b>
class Text Editor{
...
private boolean running;
Dictionary dictionary = new Dictionary();
private void startDictionary(){
try {
running = true;
if (running){
dicThread = new Thread (dictionary);
dicThread.start();
}
}
}catch (Exception e) {}
...
}
<b>
now i'd like to kill the created dictionary class without terminating the Text Editor class...
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I really don't understand what you are trying to do. If you want the new thread to stop, you should just have some variable in Dictionary like:
boolean stop = false;
public void run(){
//when this method ends, the thread you spawned dies. WHat does this method do? is it a loop?
while (!stop) {
//do stuff.
}
}
If it's not a loop, this thread will just terminate normally when it hits the end of the run method.
I think you're overthinking this "my class is a different application" thing. You're not calling main on it, so why not just treat it as you would any normal library class? Close the window of the Dictionary and you're done with it.
I still don't understand what you are "trying to kill?"
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont know if I fully understand the question.
I just combined Rob's code and yours.
correct me if I am wrong.

Dictionary Class...

Code to stop dictionary thread in TextEditor

kawaii
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic