Java is it wise to use an inner thread? if yes how to extract exceptions?
atm I am using an inner thread for calling a method. This method can throw an Exception. Is there a way to receive the Exception in the outer class to react to it? Or should I go with writing a "workthread" and adding an observer to it?
I implemented an MVC-Pattern. This this method is called in my model. Now I want to display an msg about the exception. Therefore I need to know the exception.
public void startServer(final String path, final int port, final double speedup) {
serverIsStopped = false;
new Thread() {
public void run() {
server = new SWTimeServer();
try{
server.startServer(port, speedup, path);
}catch (ClientDisconnectedException e) {
serverIsStopped = true;
//TODO
} catch (ParseException e) {
serverIsStopped = true;
//TODO
}
}
}.start();
}
I came up with this quick solution. But pretty ugly. Your opinions?
private boolean serverIsStopped = true;
private Model model = this;
public void startServer(final String path, final int port, final double speedup) {
serverIsStopped = false;
new Thread() {
public void run() {
server = new SWTimeServer();
try{
开发者_JAVA百科 server.startServer(port, speedup, path);
}catch (ClientDisconnectedException e) {
serverIsStopped = true;
model.notifyObservers(e);
} catch (ParseException e) {
serverIsStopped = true;
model.notifyObservers(e);
}
}
}.start();
}
Thanks for answers
Greetings Tarken
Your question doesn't make sense.
Your thread runs (mostly) after the calling method (which created the thread) finishes.
It wouldn't make sense to propagate an exception thrown in the thread to the outer method.
You can't receive an exception from Thread A from Thread B with any sort of basic try/catch structure that'd you'd normally use. You'll have to implement some type inter-thread messaging/signaling if you want to do something like that.
Beyond that advice, there's not much help I can give without a more complete explanation of what exactly you're trying to do.
Implement Thread.UncaughtExceptionHandler
by some class and assign it to your thread, That will allow you to receive exceptions thrown in your thread.
Thread t = new Thread() {
.....
};
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
void uncaughtException(Thread t, Throwable e) {
// Handle exception
}
});
http://download.oracle.com/javase/6/docs/api/java/lang/Thread.UncaughtExceptionHandler.html#uncaughtException(java.lang.Thread,%20java.lang.Throwable)
I haven't done much development using threads, but couldn't you react to an exception by storing the exception and joining on the main thread?
public void startServer(final String path, final int port, final double speedup) {
serverIsStopped = false;
Thread thread = new Thread() {
Exception e;
public void run() {
server = new SWTimeServer();
try{
server.startServer(port, speedup, path);
}catch (ClientDisconnectedException e) {
serverIsStopped = true;
//TODO
this.e = e;
} catch (ParseException e) {
serverIsStopped = true;
//TODO
this.e = e;
}
}
}.start();
// the main thread will wait here until the new thread terminates
thread.join();
if (thread.e != null) {
// do stuff
}
}
I would go with your own suggest of applying the observer/observable pattern here. Albeit, you will have to (loosely) couple the "worker/executor" thread object with an observer, but it would lead to much cleaner code and you can throw the necessary exception through the observer
Only problem with this approach, you don't know what state is the calling thread in, and therefore, if any action will/could be taken from the the observer's exception. Although, you could log the exception (or store it) for later retrieval by the main thread if that is dispatched again.
Hope this helps
精彩评论