As far as I understand from java.lang.Thread
docs and other questions posted here on stackoverflow, for example, “ How do I access a Runnable object by Thread? ” And “ Get the current instance of Runnable ” it is impossible to get a reference to the Runnable
object embedded in Thread
.
The scenario where such an opportunity would be useful is to implement a method whose signature cannot be changed, because we redefine the method defined in another class or interface and require different operations depending on the type of Runnable
built into the current Thread
.
If we had, for example, the getRunnable
method in the Thread
class, we could do something like this:
if (Thread.currentThread().getRunnable() instanceof Type1) { // do something... } else { // do something else... }
This can also be useful in situations where we want to ensure that the operations contained in a method are performed only by certain threads, and not by others.
So, I was wondering if there is a specific reason why Java developers decided not to allow an instance of Runnable
from Thread
, or is this a missing function that is worth notifying? If you think that there is no reason for this choice, but you should not notify him of the missing function, what strategy would you use in the scenario described above?
source share