Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Event Dispatch Thread---want to get off of it

Suppose that a method I own is sometimes called on the Event Dispatch Thread and is sometimes not. Now suppose some of the code in that method I want to have called on a thread other than the Event Dispatch Thread.

Is there a way to run some code on a thread other than the EDT at this point?

I tried this:

        if (SwingUtilities.isEventDispatchThread()) {
            new Runnable() {
                @Override
                public void run() {
                    myMethod();
                }
            }.run();
        } else {
            myMethod();
        }

But myMethod() ended up running on the EDT even when I created a new Runnable.

Is there a way to run myMethod() on a thread other than the EDT at this point?

like image 302
Paul Reiners Avatar asked Dec 21 '25 23:12

Paul Reiners


1 Answers

You doing it just fine. But your Runnable has to be pass to a new Thread.

e.g.

new Thread(new Runnable() {
 @Override
 public void run() {
     myMethod();
 }
}).start();

Please note that invoking the "run()" method won't start a new Thread. Use start() instead.

See also http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html

like image 160
Marcinek Avatar answered Dec 23 '25 14:12

Marcinek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!