Phil Hayward
Phil Hayward

Reputation: 1283

In Java, is there a version of Runnable that is intended to be executed in the current thread?

The JavaDoc for Runnable states (emphasis added)

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

Several people (including the ones who build the code analysis tool I'm using) have extrapolated from that statement the premise that Runnable should ONLY be used for classes that are intended to be executed by a separate thread. Overall, I think that was the intention of the Runnable interface, and I like to use code as intended by the author.

I want a generic interface for a class that does some work on the CURRENT thread. I'd prefer to use an industry standard if one exists. I do not need examples of how to write my own interface - I'm just interested in knowing if one already exists.

Upvotes: 2

Views: 964

Answers (5)

Solomon Slow
Solomon Slow

Reputation: 27125

java.lang.Runnable is an interface. It declares a single method, void run(). It also happens to be an @FunctionalInterface.

Technically speaking, there isn't anything else that anybody should need to know about it. If you're using a library that wants a Runnable, then you'll have no choice but to provide a Runnable. If you're implementing a library, and you want your client to provide an object that has a void run() method, then Hey! Runnable is there. Why not use it?

Stylistically speaking, you probably should only use Runnable for situations where a library wants its client to provide some method (a "task", a "callback", etc.) that the library is expected to call at a later time.

Nothing prevents you from doing this:

class MarathonRunner implements Runnable {
    ...
    void run() {
        ...
    }
}

But your fellow software geeks might think it a bit odd.

Upvotes: 0

that other guy
that other guy

Reputation: 123480

All running code is executed by a thread! It doesn't matter if it's a new thread or the current thread, it's still a thread. Here's how Callable describes it:

The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread.

The point of Runnable is that you shouldn't have to care about the threading structure, just about passing an executable piece of code.

This could be:

In our multicore world, it shouldn't matter.

Upvotes: 4

Rob
Rob

Reputation: 6497

Your interpretation of the quoted language is grammatically incorrect.

This statement is really about the Thread class, which is designed to use the Runnable interface to enable a Thread to execute arbitrary chunks of code. The statement says, essentially, that if you want to use a Thread in this way, you have to implement Runnable.

It DOES NOT say that this is the only way that Runnable should be used. In fact, the statement is completely silent on that subject.

This statement is confusing because it uses the passive voice. I think a better sentence would be something along the lines of: "To have a Thread execute some code, implement the Runnable interface and place the code in the run() method."

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136022

Take a look at java.util.concurrent.Executor.execute(Runnable command). Though it accepts Runnable, API says the Executor interface does not strictly require that execution be asynchronous. In the simplest case, an executor can run the submitted task immediately in the caller's thread

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691755

The current thread is a thread. To execute a Runnable on the current thread, simply call

myRunnable.run();

That's BTW, what some standard classes do. See for example CallerRunsPolicy

Upvotes: 3

Related Questions