Preventing the JVM from shutting down

M

Murrgon

I am running a basic swing application and I don't want the JVM to
shutdown automatically when the last window is closed. From the
documentation at JDK/docs/api/java/awt/doc-files/AWTThreadIssues.html
there is the following:

"On the other hand, if you require the JVM to continue running even
after the application has made all components undisplayable you
should start a non-daemon thread that blocks forever."

<...>
Runnable r = new Runnable() {
public void run() {
Object o = new Object();
try {
o.wait();
} catch (InterruptedException ie) {
}
}
};
Thread t = new Thread(r);
t.setDaemon(false);
t.start();
<...>

So I added this to my code and now I get this error message:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Process$1.run(Process.java:24)
at java.lang.Thread.run(Unknown Source)

Is this to be expected? What exactly is this exception telling
me? Should I catch this exception and dispose of it?

Thanks
Murrgon
 
M

Michael Borgwardt

Murrgon said:
try {
o.wait();
} catch (InterruptedException ie) {
} []
So I added this to my code and now I get this error message:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Process$1.run(Process.java:24)
at java.lang.Thread.run(Unknown Source)

Is this to be expected?

With that code, certainly.
What exactly is this exception telling
me?

That the thread that executes the code does not own the monitor
of the object o and is therefore not allowed to call its wait() method.
To own the monitor, put a

synchronized(o)
{
}

around the wait() call
 
M

Murrgon

Michael said:
Murrgon said:
try {
o.wait();
} catch (InterruptedException ie) {
}
[]

So I added this to my code and now I get this error message:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Process$1.run(Process.java:24)
at java.lang.Thread.run(Unknown Source)

Is this to be expected?


With that code, certainly.
What exactly is this exception telling
me?


That the thread that executes the code does not own the monitor
of the object o and is therefore not allowed to call its wait() method.
To own the monitor, put a

synchronized(o)
{
}

around the wait() call

Groovy, that worked. However, I now have a problem where the
JVM hangs. I have a C++ application that starts up the JVM using
JNI and then executes a java program. When the main window in
the java application is closed it tells the C++ stuff to shutdown
as well.

During the shutdown sequence on the C++ side, I have a final call
to DestroyJavaVM() once all of the other java stuff has finished.
The code execution hangs at this call. Could it be because I now
have this dummy thread in my java app that the JVM is waiting for?

Thanks
 
A

ak

Groovy, that worked. However, I now have a problem where the
JVM hangs. I have a C++ application that starts up the JVM using
JNI and then executes a java program. When the main window in
the java application is closed it tells the C++ stuff to shutdown
as well.

During the shutdown sequence on the C++ side, I have a final call
to DestroyJavaVM() once all of the other java stuff has finished.
The code execution hangs at this call. Could it be because I now
have this dummy thread in my java app that the JVM is waiting for?

try to wake up your waiting thread with notify() or notifyAll();
 
M

Murrgon

ak said:
try to wake up your waiting thread with notify() or notifyAll();

I made the 'o' Object a member of the encapsulating class and called
notify() on it at the appropriate time. It didn't change anything;
the DestroyJavaVM() call still hangs. I tried notifyAll() as well
but it didn't do anything different than notify().

Any other suggestions?
 
A

ak

I made the 'o' Object a member of the encapsulating class and called
notify() on it at the appropriate time. It didn't change anything;
the DestroyJavaVM() call still hangs. I tried notifyAll() as well
but it didn't do anything different than notify().

Any other suggestions?

sscce
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top