Some method still running when my program should be finished... how do I find out?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazedaces
    Contributor
    • May 2007
    • 284

    Some method still running when my program should be finished... how do I find out?

    My program is still running when I am finished. I outputted text at the end of the main, so I know it gets there just fine and the program finished everything just fine... but the jvm is still running. How do I find out where it's still running so I can at least know specifically what's the problem? Any special programs or ideas?

    Thanks,

    -blazed
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You must have started threads then somewhere in your program. Make those
    threads 'daemon threads' (check the API documentation) before you start them.
    Daemon threads die when the main thread dies so the JVM will stop (it has
    nothing more to do).

    kind regards,

    Jos

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Originally posted by JosAH
      You must have started threads then somewhere in your program. Make those
      threads 'daemon threads' (check the API documentation) before you start them.
      Daemon threads die when the main thread dies so the JVM will stop (it has
      nothing more to do).

      kind regards,

      Jos
      I'm sorry, but unfortunately I have 0 experience with threads. I looked into daemon threads, but honestly, I'm not starting a new thread anywhere in the program. I also checked and closed all streams, I don't know what this could be, really.

      I did find this in the class thread API:

      "When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class)."

      I could try to call this class from another program (which I'm planning on doing anyway) instead of using its own main... but I wonder if something will continue to run in the background.

      I know of threads, and a fairly good idea of what they are and how they work. I'm wondering if whatever methods I'm calling create threads (SAX ? ). I've been using sax for a while though and never had this problem before...

      Thanks for all your help,

      -blazed

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by blazedaces
        I'm sorry, but unfortunately I have 0 experience with threads. I looked into daemon threads, but honestly, I'm not starting a new thread anywhere in the program. I also checked and closed all streams, I don't know what this could be, really.

        I did find this in the class thread API:

        "When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class)."

        I could try to call this class from another program (which I'm planning on doing anyway) instead of using its own main... but I wonder if something will continue to run in the background.

        I know of threads, and a fairly good idea of what they are and how they work. I'm wondering if whatever methods I'm calling create threads (SAX ? ). I've been using sax for a while though and never had this problem before...

        Thanks for all your help,

        -blazed
        May I know
        1.) What your program does
        2.) How you determined that the JVM is still running after your program has exited

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by r035198x
          May I know
          1.) What your program does
          2.) How you determined that the JVM is still running after your program has exited
          Sure thing:

          1) My program parses an xml file and writes to another file all the "tags" in the xml file. Tags are basically locations in which characters/data are read... It also records specific pieces of data that I will need. I haven't written it yet, but afterwards, I will read the file again after the user chooses what "tags" or data they are interested in, and then store the data into a predetermined format of bytecode.

          To keep things simple, it parses the xml file and writes certain things to a file that it read. That's about it. To be even more specific, for a file that looks like this:
          Code:
          <?xml version="1.0"?>
          
          <poem xmlns="http://www.megginson.com/ns/exp/poetry">
          	<title>Roses are Red</title>
          	<l>  Roses are red,</l>
          	<l>Violets are blue;</l>
          	<l>Sugar is sweet,</l>
          	<l>And I love you.</l>
          </poem>
          the outputted text looks look like this:
          Code:
              * setDocumentLocator() called
          Parsing begins...
          Mapping starts for prefix  mapped to URI http://www.megginson.com/ns/exp/poetry
          Mapping ends for prefix 
          ...Parsing ends.
          
          Start gps-time(s): 0.0
          End gps-time(s): 0.0
          Total time(s): 0.0
          Tags:
          
          poem\title\
          poem\l\
          I actually ran the program on that file and spit out that text file.

          The tags are "poem\title \" and "poem\l\".

          2) I'm using JCreator and if you look on the top part there's an execute button and next to it or after a few more buttons there is a "stop" or kill button. You can't hit the execute button while it's still running and you can't hit the kill button unless something is running. When the program is finished the execute button is disabled and the kill button is able to be clicked. Unless I hit the kill button or put system.exit(num ber) at the end of my main it won't stop.

          Here's my main just in case it'll help:

          [code=java]
          public static void main(String args[]) {

          OutputTagsXMLRe ader XMLReader = new OutputTagsXMLRe ader();

          XMLReader.parse File();
          //I tried putting a system.out.prin tln("text") here and printed the "text" to the screen, meaning it must be finishing the .parseFile() method and all methods along the way, right?
          }
          [/code]

          Since I don't visibly start threads along the way I'm lead to believe something else in the parser perhaps is starting a thread, but I've never ran into this problem before and have been reading these xml files for quite a bit of time now since I started working here.

          Hope that answers your questions and thanks for your help,

          -blazed

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by blazedaces
            Sure thing:

            1) My program parses an xml file and writes to another file all the "tags" in the xml file. Tags are basically locations in which characters/data are read... It also records specific pieces of data that I will need. I haven't written it yet, but afterwards, I will read the file again after the user chooses what "tags" or data they are interested in, and then store the data into a predetermined format of bytecode.

            To keep things simple, it parses the xml file and writes certain things to a file that it read. That's about it. To be even more specific, for a file that looks like this:
            Code:
            <?xml version="1.0"?>
            
            <poem xmlns="http://www.megginson.com/ns/exp/poetry">
            	<title>Roses are Red</title>
            	<l>  Roses are red,</l>
            	<l>Violets are blue;</l>
            	<l>Sugar is sweet,</l>
            	<l>And I love you.</l>
            </poem>
            the outputted text looks look like this:
            Code:
                * setDocumentLocator() called
            Parsing begins...
            Mapping starts for prefix  mapped to URI http://www.megginson.com/ns/exp/poetry
            Mapping ends for prefix 
            ...Parsing ends.
            
            Start gps-time(s): 0.0
            End gps-time(s): 0.0
            Total time(s): 0.0
            Tags:
            
            poem\title\
            poem\l\
            I actually ran the program on that file and spit out that text file.

            The tags are "poem\title \" and "poem\l\".

            2) I'm using JCreator and if you look on the top part there's an execute button and next to it or after a few more buttons there is a "stop" or kill button. You can't hit the execute button while it's still running and you can't hit the kill button unless something is running. When the program is finished the execute button is disabled and the kill button is able to be clicked. Unless I hit the kill button or put system.exit(num ber) at the end of my main it won't stop.

            Here's my main just in case it'll help:

            [code=java]
            public static void main(String args[]) {

            OutputTagsXMLRe ader XMLReader = new OutputTagsXMLRe ader();

            XMLReader.parse File();
            //I tried putting a system.out.prin tln("text") here and printed the "text" to the screen, meaning it must be finishing the .parseFile() method and all methods along the way, right?
            }
            [/code]

            Since I don't visibly start threads along the way I'm lead to believe something else in the parser perhaps is starting a thread, but I've never ran into this problem before and have been reading these xml files for quite a bit of time now since I started working here.

            Hope that answers your questions and thanks for your help,

            -blazed
            Does it not open a console window written "press any key to continue" after execution?

            Comment

            • blazedaces
              Contributor
              • May 2007
              • 284

              #7
              Originally posted by r035198x
              Does it not open a console window written "press any key to continue" after execution?
              No.

              But what do you mean? Should it? Why? Do netbeans and other IDE's do that? Or are you saying my program should do that?

              -blazed

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by blazedaces
                No.

                But what do you mean? Should it? Why? Do netbeans and other IDE's do that? Or are you saying my program should do that?

                -blazed
                No, different IDE's show the output on different windows. I thought you had said something about a message being printed to the screen.
                I remember now that JCreator shows a console window somewhere at the bottom. What is the message printed there?
                I don't like having to ask for the code ...

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Just another question: do you do *anything* with AWT or Swing in your program?

                  kind regards,

                  Jos

                  Comment

                  • blazedaces
                    Contributor
                    • May 2007
                    • 284

                    #10
                    Originally posted by r035198x
                    No, different IDE's show the output on different windows. I thought you had said something about a message being printed to the screen.
                    I remember now that JCreator shows a console window somewhere at the bottom. What is the message printed there?
                    I don't like having to ask for the code ...
                    The message simply says this:

                    Code:
                    --------------------Configuration: <Default>--------------------
                    Finished Writing Tag Log File
                    Sorry, I took out that message that I said I was writing. I just wrote it to the screen at one point to verify that the program got to that stage. If I'm misunderstandin g what you mean could you specify what message of mine you're referring to?

                    Listen, if this was shorter I would post all my code, but it's a little too much to put all on the forum. The reader class isn't that big and this is it:

                    [code=java]/**
                    * @(#)PrettyPrint XMLReader.java
                    *
                    *
                    * @author
                    * @version 1.00 2007/5/14
                    */

                    import java.util.*;
                    import java.io.*;
                    import org.xml.sax.*;
                    import org.xml.sax.hel pers.XMLReaderF actory;
                    import java.awt.*;
                    import AsafUtil.OnlyEx t;

                    public class OutputTagsXMLRe ader {
                    private String fileName = "";

                    public String fileName() { return this.fileName; }

                    private FilenameFilter filter = new OnlyExt("xml");

                    FileReader FID;

                    // Initiate handler
                    public OutputTagsHandl er handler;

                    public OutputTagsXMLRe ader() {
                    FileDialog fd = new FileDialog(new Frame(), "Choose a .out file to open...", FileDialog.LOAD );
                    fd.setFilenameF ilter(filter);
                    fd.setFile("*.x ml");
                    fd.setLocation( 50, 50);
                    fd.setVisible(t rue);

                    fileName = fd.getDirectory () + System.getPrope rty("file.separ ator") + fd.getFile();

                    handler = new OutputTagsHandl er(fd.getFile() );
                    }

                    public static void main(String args[]) {

                    OutputTagsXMLRe ader XMLReader = new OutputTagsXMLRe ader();

                    XMLReader.parse File();
                    }

                    // parseFile chooses the filename and then reads the file and sends it
                    // to parseFile(FID)
                    public void parseFile() {
                    FID=null;
                    try {
                    if (!fileName.equa ls(""))
                    FID = new FileReader(file Name);
                    else {
                    System.out.prin tln("Filename not inputted, can't parse... exiting");
                    try {
                    FID.close();
                    } catch (IOException e) {
                    System.out.prin tln("Error trying to close FID:\n");
                    e.printStackTra ce();
                    System.out.prin tln();
                    }
                    System.exit(0);
                    }
                    parseFile(FID);
                    }
                    catch (FileNotFoundEx ception e) {
                    System.out.prin tln("File not found exception: ");
                    e.printStackTra ce();
                    } finally {
                    try {
                    FID.close();
                    } catch (IOException e) {
                    System.out.prin tln("Error trying to close FID:\n");
                    e.printStackTra ce();
                    System.out.prin tln();
                    }
                    }
                    }

                    // This method initiates the XMLReader, sets the handlers, and finally,
                    // parses the file.
                    //
                    // @param = "FID" - FileReader of a prechosen filename that is to be parsed
                    public void parseFile(FileR eader FR) {
                    try {
                    // Create the XML reader;
                    XMLReader reader = null;
                    try {
                    reader = XMLReaderFactor y.createXMLRead er();
                    } catch (SAXException e) {
                    System.out.prin tln("Failed to create XMLReader:\n");
                    e.printStackTra ce();
                    System.exit(1);
                    }

                    // Assign the handlers
                    reader.setConte ntHandler(handl er);
                    reader.setError Handler(handler );

                    // Parse the document
                    InputSource systemId = new InputSource(FR) ;
                    try {
                    reader.parse(sy stemId);
                    } catch (SAXException e1) {
                    System.out.prin tln(systemId +
                    " failed with XML error:\n");
                    e1.printStackTr ace();
                    } catch (IOException e2) {
                    System.out.prin tln(systemId +
                    " failed with I/O error:\n");
                    e2.printStackTr ace();
                    }
                    System.out.prin t("\n");
                    } finally {
                    try {
                    FR.close();
                    } catch (java.io.IOExce ption e) {
                    System.out.prin tln("Unable to close FR, must exit...");
                    e.printStackTra ce();
                    System.exit(1);
                    }
                    }
                    }
                    }[/code]

                    Thanks for all your help and sorry for the misunderstandin g,

                    -blazed

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Got it. As I suspected (see my previous reply) you are using the AWT event
                      dispatcher thread (you're using a FileDialog). That thread isn't a daemon thread
                      so it keeps on running after the main thread died. Terminate your program with
                      a System.exit(<so me number>) and all will be fine again.

                      kind regards,

                      Jos

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Most probably the parsing is not completing then.The parse method is synchronous. see the specs for it.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by JosAH
                          Got it. As I suspected (see my previous reply) you are using the AWT event
                          dispatcher thread (you're using a FileDialog). That thread isn't a daemon thread
                          so it keeps on running after the main thread died. Terminate your program with
                          a System.exit(<so me number>) and all will be fine again.

                          kind regards,

                          Jos
                          Oh, I missed that thread.

                          Comment

                          • blazedaces
                            Contributor
                            • May 2007
                            • 284

                            #14
                            Originally posted by JosAH
                            Got it. As I suspected (see my previous reply) you are using the AWT event
                            dispatcher thread (you're using a FileDialog). That thread isn't a daemon thread
                            so it keeps on running after the main thread died. Terminate your program with
                            a System.exit(<so me number>) and all will be fine again.

                            kind regards,

                            Jos
                            Well, here's the thing. I want to call this class, run it, then continue running a program which will read that file, and reparse for specific data to turn it into bytecode. If I system.exit(0) won't it stop everything? I know I could just let that old file dialog run in the background, but it seems like a waste of processor? Is there a way to specifically stop this class/program from running and continue the rest?

                            ... Am I going to have to run it as a thread and kill it when it's finished or something like that?

                            -blazed

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by blazedaces
                              Well, here's the thing. I want to call this class, run it, then continue running a program which will read that file, and reparse for specific data to turn it into bytecode. If I system.exit(0) won't it stop everything? I know I could just let that old file dialog run in the background, but it seems like a waste of processor? Is there a way to specifically stop this class/program from running and continue the rest?

                              ... Am I going to have to run it as a thread and kill it when it's finished or something like that?

                              -blazed
                              Yep, if you System.exit(<nu mber>) everything dies. About that AWT thread:
                              the thread doesn't do anything at all (read: doesn't take up cpu time) when there
                              are no user instigated events and nothing needs to be repainted, so you can let
                              it run forever. It won't stop running when your main thread dies though, hence the
                              System.exit(<nu mber>).

                              You won't lose performance because of that (waiting) thread. It was a design
                              issue for the designers of AWT/Swing: either have a multi threaded GUI thing,
                              which is a total mess (google for that) or have a single threaded, non daemon
                              thread take care of it all. You don't even want to terminate that thread when your
                              main thread terminates, i.e. it would be nearly impossible to build GUI applications
                              if that were the case.

                              Simply let that thread wait and invoke System.exit(<nu mber>) when your main
                              thread dies. If you're really paranoia about it: check the ThreadGroup an use the
                              (deprecated) stop() method on the AWT thread.

                              kind regards,

                              Jos

                              Comment

                              Working...