This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Simply put, I would like to end a thread without ending the JVM. However, the problem is more complex (at least for me). I'll try to describe how the application is built, and then hopefully someone will know the solution.
I need to run code is matlab from the a java gui. What I do is from Matlab, create a new class. This class is responsible from creating a new thread into which the application is launched. If I don't create my application/thread in this way, Matlab will wait for my program to complete, so I won't be able to interact with Matlab once the program is launched. From this new thread, I launch a variety of frames which is all controlled by one generic (java.lang.Object) class.
Once all the windows are closed, there is no user interaction, so the application should close. I can't call System.exit() because this will also shutdown Matlab, so I need some way of ending the thread from within the thread.
I tried thread.stop() from within the thread (pass in the thread as an argument), but when testing in Eclipse, I can see that the memory usage of the javaw.exe process (for my application) is unchanged, so I suspect the application is still running.
Is there an alternative to calling System.exit() or is this exactly what I should do?
Joined: Nov 11, 2004
I forgot to add, I am using JVM 1.4.2, which I can not change. (A further restriction of Matlab)
A thread will end when its run method exits. The handle to the thread is typically still held in the system, but its of no concern. the javaw.exe processes memory usage is not an indicator of memory distribution within the JVM. There are a few System calls in java that can give you a more accurate picture of what the JVM is doing.
I actually do not understand why you are starting and stopping a thread. How many programs are there here, and who is starting what and why?
matlab is a java package that you are starting in a seperate thread?
Joined: Nov 11, 2004
I suppose I may have been unsuccessful in my description, so I'll try to add some more detail. First I'll talk about what matlab is and my interface to it, talk about some details of my software, and then try to rephrase my question.
MATLAB AND THE INTERFACE TO IT
Matlab is commerical programming software usually used for matrix calculations.
I have a separate software, similar to Flowmaster, which requires the capabilities of matlab for complex matrix calculations. I have developed a GUI using the JGraph component to enter the data.
I can then launch my application from within Matlab, and from there, send data and commands back to Matlab to perform the analysis that is required. (It is not possible to make this link in any other way).
When I launch my application (from within the Matlab GUI), the first thing I need to do is create a new thread, so that the user can continue to interact separately with Matlab, while concurrently interacting with my software. If I do not create a new thread, the user was be unable to interact with Matlab until my application is closed. Basically, my application is encapsulated within a separate thread from the thread running the Matlab GUI.
Now, eventually, the user may wish to exit my application (in the normal way from within the application), but not exit Matlab. However, if I call
the matlab will also shutdown because the JVM exits. So what I need is some way to the thread for my software, without exiting the JVM.
In essence, you are correct in seeing two separate programs, that are sharing one instance of the JVM.
My application itself it designed similar to MS Word (Single document interface). There can be several windows (frames), but there is only one instance of the application. These various frames are all linked together via a common "application" class, which is just a generic , which handles creating windows, event redirection to the currently selected window, etc.
Thus, when I create my application from within matlab, I create an instance of this class which in addition to many other things common to all of the possible SDIs, creates an initial SDI.
MY QUESTION I understand that when the run method exits, the thread is complete, but I am unsure when this occurs. It's not as if there is a clear end point as in a modal dialog.
So here's my question(s). When does my thread die, and how can I kill it from within itself?
How exactly is matlab starting your application? How does information get from your started application back into matlab?
Joined: Nov 11, 2004
Thank you for replying again.
I'm posting parts of three files: the m-file which is how I launch my application from matlab, the class that creates the new thread, and the "hidden linking" class that holds the various JFrames together).
From matlab, I use the following m-file (like a script) to launch my application. The file does two things. First, it finds the directory where my application is located. Essentially, Matlab takes control of the directory returned by Object.getClass().getResource(), so I pass in a string that tells my application where it is located. (Otherwise, I will get the Matlab "current directory.") The second thing the m-file does is lanuch the application. (Comments are deonted by everything following '%' on a line)
From here, you can see that I call the main(String args) function of the Main class. As I said previously, this creates a new thread which launches the application. (The class that it launches is designed as a Singleton).
You can see that nothing is specifically passed to my application that permits me to interact with the matlab environment. However, doing it this way permits me to create an instance of com.mathworks.jmi.Matlab, through which I can sends commands/data back to matlab (see Calling matlab from java and MatlabControl.java).
On a side note, while I believe it is possible to return data from Matlab back to my application through return values of function calls, I have prefer to 'push' the data into my application by designing a few special functions. So, whenever I send a command to Matlab, I always include a reference to my application to facilitate the data transfer. This way I control what type of data is passed (Matlab itself is weakly typed).
I'm also posting a snippit of code from the Editor class, in case you find that useful. I've removed a significant amount of code to focus on the part that I think are more relevant. (You can see that my current way to exit is System.exit(1), which is what I want to change.)
Thanks again for your help.
[ November 14, 2004: Message edited by: Blair Fick ] [ November 14, 2004: Message edited by: Blair Fick ]
Have you tried just omitting the System.exit () call? Or did you mean that you're not sure the java program is really finished? Maybe there is still a vestige of a Swing loop running even if all the java editor windows have closed.
Not sure that helps - just some thoughts.
The nice thing about Standards is that there are so many to choose from!
Joined: Nov 11, 2004
That is precisely what I think. I can set everything to null, but I suspect that something may still exist. In particular, I think any static final variables will still exist (???). Since everything is encapsulated within a thread, I thought there may be some way to safely end that thread, and that might be the best way to end my software.
However, even this way I'm unconvinced that everything will be totally destroyed, because I think there is only one Swing event thread shared between my application and Matlab (creating a modal dialog blocks both my application and Matlab). That also seems to me to be where the Editor class exists, since with my limited understanding of Threads, the run method must complete. Thus, ending the thread I create may not achieve my goal of destroying everything related to my application.
1. I am not sure why you are starting a new thread in yoru main method. Are you sure you gain anything here? Its not doing anything.
2. What is the poing of the singleton? Everytime you execute that script you will get another GUI anyway because its gonna start a new JVM. If Editor.instance().addEditor is only called from one place in your code, the 'singleton' is wasted.
3. GUIs run in their own set of threads, so you can't force them to quit per se. An application is supposed to have only 1 JFrame. If that is the case you can call JFrame.setDefaultCloseOperation(EXIT_ON_CLOSE); but if you plan to have more JFrames opening after the first one is close you can try JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE)
4. sUN was riding the fence on what static variables should do when no active instances of their classes were around. I cant recall what they chose, but static variales and objects will not keep the jvm from exiting if they can not be reached.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com