How can I switch back to main thread from a different thread when there is an exception. When an exception is raised on a child thread, I want a notification to be sent to main thread and execute a method from the main thread. How can I do this?
Thanks.
Additional information
I am calling a method from my main method and starting a new thread there after some calculations and changes
Thread thread = new Thread() {
@Override
public void run() {
.....
}
}
thread.start();
When the exception occurs in the child thread, what is the main thread going to be doing? It will have to be waiting for any errors in a child thread.
You can establish an UncaughtExceptionHandler
in the child thread, which can raise an event that the main thread is waiting for.
As TofuBeer says you need to provide more context, if however that context is that you're a swing app...
SwingUtilities.invokeLater(Runnable r) allows you to call back on Swing's main execution thread.
} catch (final Exception e) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
//do whatever you want with the exception
}
});
}
If your child thread was created in the main, you might leave the exception pop up and handle it on the main thread.
You can also put a sort of call back.
I haven't tried this my self though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With