views:

840

answers:

5

Hi

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();
+1  A: 

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.

OscarRyz
+3  A: 

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.

Hemal Pandya
It doesn't have to be waiting. It could periodically poll a queue for an error event, moving along to other tasks if there are no events to be handled.
erickson
@eickson, yes it doesn't need to be waiting all the time, but it will have wait at some point. I wanted to make sure the OP understood this will have to be a wait/notify, that Thread.start does not return a value that can be used by caller.
Hemal Pandya
This method (and my method) do not actually get executed on the main thread, they are still done with Thread.currentThread == the thread where the exception occurs. Not sure if that really matters to the OP.
TofuBeer
@TofuBeer: Which method (mine) are you referring to? As I said, the exception handler should raise an event that main thread. The call back you describe will not work by itself.
Hemal Pandya
I said that any method won't work - that includes mine! What I was referring to is that UncaughtExceptionHandler executes on the child thread not the main thread. AFAIK there is no way to get back onto the main thread.
TofuBeer
Sorry, your reference to "this method" confused me.But of course there is a way to go get back onto the main thread. Raise an event in the child thread and catch it in the main.
Hemal Pandya
you cannot do that, run does not throw an exception... well ok you can as a RuntimeException but you should not be catching those.
TofuBeer
+1  A: 

This might work for you.

public class Main
{
    public static void main(final String[] argv)
    {
        final Main main;

        main = new Main();
        main.go();
    }

    public void go()
    {
        final Runnable runner;
        final Thread   thread;

        runner = new Foo(this);
        thread = new Thread(runner);
        thread.start();
    }

    public void callback()
    {
        System.out.println("hi!");
    }
}

class Foo
    implements Runnable
{
    private final Main main;

    Foo(final Main m)
    {
        main = m;
    }

    public void run()
    {
        // try catch and handle the exception - the callback is how you notify main
        main.callback();
    }
}
TofuBeer
Completely bull. Classes != threads. You don’t notify any other thread in your code.
Bombe
Correct, and I did ask him the question of more context (but never got a usable answer). The code above does let you notify Main that something went wrong. Note that I said it _might_ work for him - cannot say for certain since what he wants isn't possible (AFAIK)
TofuBeer
+1  A: 

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
        }
    }); 
}
Tom
Yes, how you do the callback depends very much on the way the program is running.
TofuBeer
A: 

Implement the interface http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html in some class of your application, and let it deal with the thread and the exception it generated.

See the document for more details.

EDIT: This is probably not what you wanted - I believe the method is going to be called by the thread that just threw the exception. If you want some method to be executed on your main thread (in its context), the main thread will have to be waiting for it somehow.

But if you only want to handle the exception that's one case. The best approach, thought, would be to handle the exception in a try...catch block inside your thread.

Ravi Wallau