How to quit with a locked thread around ?

  • Thread starter Patrick Stadelmann
  • Start date
P

Patrick Stadelmann

Hi,

How can I force my application to quit when it has a "locked" thread ?
The lockup is caused by attempting to use a Virtual COM port when no
device is connected. I can detect the lockup and live with it, however
it prevents my application from exiting when I return from "main".

Thanks,

Patrick
 
O

Owen Jacobson

Hi,

How can I force my application to quit when it has a "locked" thread ?
The lockup is caused by attempting to use a Virtual COM port when no
device is connected. I can detect the lockup and live with it, however
it prevents my application from exiting when I return from "main".

In addition to exiting when all non-daemon threads terminate, the JVM
will also exit when certain methods are called:

<http://java.sun.com/javase/6/docs/api/java/lang/
System.html#exit(int)>

-o
 
K

Knute Johnson

Patrick said:
Hi,

How can I force my application to quit when it has a "locked" thread ?
The lockup is caused by attempting to use a Virtual COM port when no
device is connected. I can detect the lockup and live with it, however
it prevents my application from exiting when I return from "main".

Thanks,

Patrick

The best option would be to get the 'locked' thread to return. Maybe
you could timeout the call to use the COM port and force the return.
The other option is to specifically call System.exit() when you are done.
 
T

Thomas Kellerer

Patrick Stadelmann wrote on 07.05.2008 22:02:
Hi,

How can I force my application to quit when it has a "locked" thread ?
The lockup is caused by attempting to use a Virtual COM port when no
device is connected. I can detect the lockup and live with it, however
it prevents my application from exiting when I return from "main".
Run that thread as a daemon thread. After creating the Thread instance and
before starting the thread call setDaemon(true)
 
P

Patrick Stadelmann

Owen Jacobson said:
In addition to exiting when all non-daemon threads terminate, the JVM
will also exit when certain methods are called:

<http://java.sun.com/javase/6/docs/api/java/lang/
System.html#exit(int)>

This worked, thanks.

Knute Johnson said:
The best option would be to get the 'locked' thread to return. Maybe
you could timeout the call to use the COM port and force the return.

I tried this first, but to no avail. The lockup occurs when writing to
the output stream with certain Virtual COM port, and I couldn't find a
way to set the write operation to timeout.
The other option is to specifically call System.exit() when you are done.

Thanks, it worked.
Run that thread as a daemon thread. After creating the Thread instance and
before starting the thread call setDaemon(true)

This did not work, but thanks anyway !

Patrick
 
A

Arne Vajhøj

Patrick said:
This worked, thanks.

But it is a rather hard way of closing down. It may be fine
for your app, but ther are certinly apps where it would
not be good.

Arne
 
C

Chronic Philharmonic

Unfortunately, when a thread is running deep in the guts of native code, it
might not respond to standard Java thread attention getters, such as
interrupting the thread or marking it as a daemon. For example, this can be
a problem for threads blocked in socket reads. Sometimes you can break the
thread loose by closing the socket from another thread, but it can be
implementation and/or platform dependent even then.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,274
Latest member
JessMcMast

Latest Threads

Top