473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

blazedaces
284 100+
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
Jul 16 '07 #1
18 2134
JosAH
11,448 Expert 8TB
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
Jul 16 '07 #2
blazedaces
284 100+
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
Jul 16 '07 #3
r035198x
13,262 8TB
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
Jul 16 '07 #4
blazedaces
284 100+
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:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <poem xmlns="http://www.megginson.com/ns/exp/poetry">
  4.     <title>Roses are Red</title>
  5.     <l>  Roses are red,</l>
  6.     <l>Violets are blue;</l>
  7.     <l>Sugar is sweet,</l>
  8.     <l>And I love you.</l>
  9. </poem>
  10.  
the outputted text looks look like this:
Expand|Select|Wrap|Line Numbers
  1.     * setDocumentLocator() called
  2. Parsing begins...
  3. Mapping starts for prefix  mapped to URI http://www.megginson.com/ns/exp/poetry
  4. Mapping ends for prefix 
  5. ...Parsing ends.
  6.  
  7. Start gps-time(s): 0.0
  8. End gps-time(s): 0.0
  9. Total time(s): 0.0
  10. Tags:
  11.  
  12. poem\title\
  13. poem\l\
  14.  
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(number) at the end of my main it won't stop.

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

Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) {
  2.  
  3.         OutputTagsXMLReader XMLReader = new OutputTagsXMLReader();
  4.  
  5.         XMLReader.parseFile();
  6.         //I tried putting a system.out.println("text") here and printed the "text" to the screen, meaning it must be finishing the .parseFile() method and all methods along the way, right?
  7.     }
  8.  
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
Jul 16 '07 #5
r035198x
13,262 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  
  3. <poem xmlns="http://www.megginson.com/ns/exp/poetry">
  4.     <title>Roses are Red</title>
  5.     <l>  Roses are red,</l>
  6.     <l>Violets are blue;</l>
  7.     <l>Sugar is sweet,</l>
  8.     <l>And I love you.</l>
  9. </poem>
  10.  
the outputted text looks look like this:
Expand|Select|Wrap|Line Numbers
  1.     * setDocumentLocator() called
  2. Parsing begins...
  3. Mapping starts for prefix  mapped to URI http://www.megginson.com/ns/exp/poetry
  4. Mapping ends for prefix 
  5. ...Parsing ends.
  6.  
  7. Start gps-time(s): 0.0
  8. End gps-time(s): 0.0
  9. Total time(s): 0.0
  10. Tags:
  11.  
  12. poem\title\
  13. poem\l\
  14.  
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(number) at the end of my main it won't stop.

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

Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) {
  2.  
  3.         OutputTagsXMLReader XMLReader = new OutputTagsXMLReader();
  4.  
  5.         XMLReader.parseFile();
  6.         //I tried putting a system.out.println("text") here and printed the "text" to the screen, meaning it must be finishing the .parseFile() method and all methods along the way, right?
  7.     }
  8.  
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?
Jul 16 '07 #6
blazedaces
284 100+
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
Jul 16 '07 #7
r035198x
13,262 8TB
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 ...
Jul 16 '07 #8
JosAH
11,448 Expert 8TB
Just another question: do you do *anything* with AWT or Swing in your program?

kind regards,

Jos
Jul 16 '07 #9
blazedaces
284 100+
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:

Expand|Select|Wrap|Line Numbers
  1. --------------------Configuration: <Default>--------------------
  2. 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 misunderstanding 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:

Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * @(#)PrettyPrintXMLReader.java
  3.  *
  4.  *
  5.  * @author 
  6.  * @version 1.00 2007/5/14
  7.  */
  8.  
  9. import java.util.*;
  10. import java.io.*;
  11. import org.xml.sax.*;
  12. import org.xml.sax.helpers.XMLReaderFactory;
  13. import java.awt.*;
  14. import AsafUtil.OnlyExt;
  15.  
  16. public class OutputTagsXMLReader {
  17.     private String fileName = "";
  18.  
  19.     public String fileName() { return this.fileName; }
  20.  
  21.     private FilenameFilter filter = new OnlyExt("xml");
  22.  
  23.     FileReader FID;
  24.  
  25.     // Initiate handler
  26.     public OutputTagsHandler handler;
  27.  
  28.     public OutputTagsXMLReader() {
  29.         FileDialog fd = new FileDialog(new Frame(), "Choose a .out file to open...", FileDialog.LOAD);
  30.         fd.setFilenameFilter(filter);
  31.         fd.setFile("*.xml"); 
  32.         fd.setLocation(50, 50);
  33.         fd.setVisible(true);
  34.  
  35.         fileName = fd.getDirectory() + System.getProperty("file.separator") + fd.getFile();
  36.  
  37.         handler = new OutputTagsHandler(fd.getFile());
  38.     }
  39.  
  40.     public static void main(String args[]) {
  41.  
  42.         OutputTagsXMLReader XMLReader = new OutputTagsXMLReader();
  43.  
  44.         XMLReader.parseFile();
  45.     }
  46.  
  47.     // parseFile chooses the filename and then reads the file and sends it
  48.     // to parseFile(FID)
  49.     public void parseFile() {
  50.         FID=null;
  51.         try {
  52.             if (!fileName.equals(""))
  53.                 FID = new FileReader(fileName);
  54.             else {
  55.                 System.out.println("Filename not inputted, can't parse... exiting");
  56.                 try {
  57.                     FID.close();
  58.                 } catch (IOException e) {
  59.                     System.out.println("Error trying to close FID:\n");
  60.                     e.printStackTrace();
  61.                     System.out.println();
  62.                 }
  63.                 System.exit(0);
  64.             }
  65.             parseFile(FID);
  66.         }
  67.         catch (FileNotFoundException e) {
  68.             System.out.println("File not found exception: ");
  69.             e.printStackTrace();
  70.         } finally {
  71.             try {
  72.                 FID.close();
  73.             } catch (IOException e) {
  74.                 System.out.println("Error trying to close FID:\n");
  75.                 e.printStackTrace();
  76.                 System.out.println();
  77.             }
  78.         }
  79.     }
  80.  
  81.     // This method initiates the XMLReader, sets the handlers, and finally,
  82.     // parses the file.
  83.     //
  84.     // @param = "FID" - FileReader of a prechosen filename that is to be parsed
  85.     public void parseFile(FileReader FR) {
  86.         try {
  87.             // Create the XML reader;
  88.             XMLReader reader = null;
  89.             try {
  90.                 reader = XMLReaderFactory.createXMLReader();
  91.             } catch (SAXException e) {
  92.                 System.out.println("Failed to create XMLReader:\n"); 
  93.                 e.printStackTrace();
  94.                 System.exit(1);
  95.             }
  96.  
  97.             // Assign the handlers    
  98.             reader.setContentHandler(handler);
  99.             reader.setErrorHandler(handler);
  100.  
  101.             // Parse the document
  102.             InputSource systemId = new InputSource(FR);
  103.             try {
  104.                 reader.parse(systemId);
  105.             } catch (SAXException e1) {
  106.                 System.out.println(systemId +
  107.                            " failed with XML error:\n");
  108.                            e1.printStackTrace();
  109.             } catch (IOException e2) {
  110.                 System.out.println(systemId +
  111.                            " failed with I/O error:\n");
  112.                            e2.printStackTrace();
  113.             }
  114.             System.out.print("\n");
  115.         } finally {
  116.             try {
  117.                 FR.close();
  118.             } catch (java.io.IOException e) {
  119.                 System.out.println("Unable to close FR, must exit...");
  120.                 e.printStackTrace();
  121.                 System.exit(1);
  122.             }
  123.         }
  124.     }
  125. }
Thanks for all your help and sorry for the misunderstanding,

-blazed
Jul 16 '07 #10
JosAH
11,448 Expert 8TB
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(<some number>) and all will be fine again.

kind regards,

Jos
Jul 16 '07 #11
r035198x
13,262 8TB
Most probably the parsing is not completing then.The parse method is synchronous. see the specs for it.
Jul 16 '07 #12
r035198x
13,262 8TB
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(<some number>) and all will be fine again.

kind regards,

Jos
Oh, I missed that thread.
Jul 16 '07 #13
blazedaces
284 100+
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(<some 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
Jul 16 '07 #14
JosAH
11,448 Expert 8TB
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(<number>) 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(<number>).

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(<number>) 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
Jul 16 '07 #15
blazedaces
284 100+
Yep, if you System.exit(<number>) 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(<number>).

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(<number>) 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
Thanks a lot. I guess I'll just use the system.exit(<number>) whenever I'm completely finished...

-blazed
Jul 16 '07 #16
JosAH
11,448 Expert 8TB
Thanks a lot. I guess I'll just use the system.exit(<number>) whenever I'm completely finished...

-blazed
Yup, that's the way to go or otherwise don't start up that AWT thread at all,
i.e. don't instantiate any AWT/Swing component at all. That 'setVisible()' method
is the culprit: it enqueues a repaint operation which is the job of that AWT thread.

kind regards,

Jos
Jul 16 '07 #17
blazedaces
284 100+
Yup, that's the way to go or otherwise don't start up that AWT thread at all,
i.e. don't instantiate any AWT/Swing component at all. That 'setVisible()' method
is the culprit: it enqueues a repaint operation which is the job of that AWT thread.

kind regards,

Jos
What better way is there to ask the user what file to read or save to? Is there a way to make the file dialog appear without using setVisible? How can the AWT thread do it on it's own?

-blazed
Jul 16 '07 #18
JosAH
11,448 Expert 8TB
What better way is there to ask the user what file to read or save to? Is there a way to make the file dialog appear without using setVisible? How can the AWT thread do it on it's own?

-blazed
Nothing 'appears' out of itself without making it visible first. Either bail out if the
user didn't supply the correct command line argument(s) or use a shell or command
line interface for the correct arguments or go for the whole nine yards and
supply a gui (in which case you have to System.exit()).

kind regards,

Jos
Jul 16 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello I am teaching myself python, and I have gotten a long way, it's quite a decent language and the syntax is great :) However I am having a...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
1
by: Venkat | last post by:
I just installed Visual Studio.Net 2003 and that is supposed to add that asp.net 1.1 extension. I got an error message while opening ASP.NET project.Error is : "visual studio.net has...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
5
by: Niall | last post by:
I have an unmanaged C++ ray tracer which I am working to put a C# front end on. It runs fine as just the unmanaged code. I have made a MC++ wrapper DLL to expose the required types to the C#...
6
by: TS | last post by:
Im in a web page and call an asynchronous method in business class. the call back method is in the web page. When page processes, it runs thru code begins invoking the method then the page...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
0
by: Svenn Bjerkem | last post by:
Hi, Armed with Programming Python 3rd Edition and Learning Python 2nd edition I try to write an application which I at first thought was simple, at least until I was finished with the GUI and...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.