Bad practice to use Runnable as callback / subroutine?
Asked Answered
A

2

14

Is it's considered bad practice to use Runnable as a callback?

Considering that Runnable is meant to be used with threads (see it's JavaDoc), I'm wondering if this is okay - or whether I should make my own interface for this purpose.

What I'm talking about is something like:

public class KeyBinding {
    public KeyBinding(KeyStroke stroke, Runnable handler) {
        //...
    }
}
Aspect answered 28/3, 2014 at 21:35 Comment(0)
H
7

Actually, Runnables can be used for any purpose.

"The general contract of the method run is that it may take any action whatsoever"(Runnable javadoc)

Generally, it should not be bad practice, definitely better practice than creating an extra unnecessary interface in your own code.

Haematozoon answered 28/3, 2014 at 21:57 Comment(2)
On the other hand, looking at the doc link you give, it says The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. So, as KajMagnus experienced, people might expect the Runnable to be associated with a thread, and find this usage confusing. I agree with KajMagnus that defining an Interface specific to this usage is clearer.Aisne
Actually, It is always associated with a thread, the main thread!Rhu
L
27

Don't use Runnable as a callback; it might cause confusion: people and code quality tools sometimes expect it to be used with threads only.

I did myself use Runnable as a callback — I thought it seemed fairly well suited for use as a generic callback. A month later someone found my code snipped:

doneCallback.run();

and he noticed that the doneCallback was a Runnable, and that invoking .run() directly resulted in a warning in our code quality analysis program (Sonar). So, to fix the warning?, or because he thought the intention was to create a thread?, he forked a new thread, and called run() via that thread instead.

However, forking a thread there, broke stuff.

To avoid confusion, now I'm instead creating a generic callback interface that's not related to threads in any way. I'm just adding a class Callback with a method call. I think I'd better not use java.util.concurrent.Callback because that one is related to threads too.

Leclair answered 12/5, 2015 at 6:53 Comment(1)
This has been really helpful! thanx a lot ^^Regrate
H
7

Actually, Runnables can be used for any purpose.

"The general contract of the method run is that it may take any action whatsoever"(Runnable javadoc)

Generally, it should not be bad practice, definitely better practice than creating an extra unnecessary interface in your own code.

Haematozoon answered 28/3, 2014 at 21:57 Comment(2)
On the other hand, looking at the doc link you give, it says The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. So, as KajMagnus experienced, people might expect the Runnable to be associated with a thread, and find this usage confusing. I agree with KajMagnus that defining an Interface specific to this usage is clearer.Aisne
Actually, It is always associated with a thread, the main thread!Rhu

© 2022 - 2024 — McMap. All rights reserved.