views:

106

answers:

7

I'm confused (new to java):

When implementing the Runnable interface, one must override the run() method to get thread execution capability. Implementing this interface makes your object a type Runnable (?). How does the thread functionality get "injected" by simply implementing the Runnable interface? Basically when you instantiate a class that implements Runnable, what is going on that ties in the threading functionality? I am probably misunderstanding some basing OO concept here. Thanks.

Is it the JVM that "knows" to look for a runnable when doing a thread.start()?

+4  A: 

Runnable represents a piece of work that "can" run in a separate thread or not (you could just call Runnable.run() yourself).

However to invoke the Runnable in a separate thread then do something like

Thread thread = new Thread(new MyRunnable());
thread.start(); // MyRunnable will now be invoked in a new thread
Mike Q
+2  A: 

Nothing special is happening in the background really.

Implementing the Runnable interface ensure that your class will have a public void run() method.

The magic really happens when you pass your custom class into a Thread.

Thread th = new Thread(new YourCustomRunnable());
th.start();

In the above code, a new Thread will be created, and the code inside the run() method will be ran in another thread.

Internally the thread will call your custom run() method and make that code run on a separate thread. It is technically possible to do the following:

Runnable r = new MyCustomRunnable();
r.run();

In the above code, r will not run on a separate thread.

jjnguy
+8  A: 

When you create an implementation of Runnable, nothing ties your class to the thread capacities of the JVM. An instance of the Runnable interface is like an instance of any other interface, just another instance.

If you want to use the threading system of the JVM, you have to use a new instance of the Thread class, that will run the run() method of your Runnable implementation in a separate thread.

All the logic about creating a new thread is done by the Thread class.

Vivien Barousse
ah there it is. The key is that the Thread class' constructor expects a Runnable param. Thanks!
ikp
@ikp, If this answer helped you, you can validate it. (The check right under the vote count)
Colin Hebert
I will..when the system lets me :)
ikp
A note I'd like to add here: all that a thread ever needs to know is what it is supposed to do. If you can somehow hand a "function" to a `Thread` object, it should be able to "execute" it in a thread. Since in java you cannot have free floating function objects, you need to wrap it up in a conformant interface (here, a `Runnable`). In a functional-programming-capable language, you would just hand a function to a `Thread`-like class.
Here Be Wolves
If only functions were first class objects in java ..
ikp
+1  A: 

An interface is a kind of contract. By implementing Runnable you promise that you will provide all the methods defined in the interface. So any other class like "Thread" which knows about Runnable.run() can call this method on object of your class. Even without knowing anything about your class.

In order to start a new thread with your code you need to write something like this:

 Thread thread = new Thread(new MyRunnable());
 thread.start();

The start() method will do some operating system magic in order to generate the thread, and then call the run() method in the context of that operating system thread on the object you provided as parameter to the constructor.

nhnb
+2  A: 

Implementing Runnable doesn't start a new thread, to start a new thread you need to create a new Thread object and start it, and one of the most used constructors of Thread takes a Runnable as parameter (Thread(Runnable)):

Thread t = new Thread(new MyRunnable());
t.start();
Ssancho
+1  A: 

There is not "injection" in terms of terms of recent definitions (spring/DI). Runnable is no different than any other interface. It is "a contract" that states your class provides any necessary methods called out in the interface.

chrismh
You could think of it as constructor injection of a Runnable into a Thread. There's no DI engine except for your code providing the dependency.
duffymo
I guess, although in my mind, thats stretching the injection definition to its limits... :)
chrismh
+2  A: 

Implementing Runnable doesn't magically make your class run on a thread. Instead, you can then do something like:

Runnable myRunnable = new MyRunnable(); // MyRunnable implements Runnable
Thread t = new Thread(myRunnable);
t.start();

Now your code within the run() method of myRunnable will execute in a separate thread. I'd urge you to review the ExecutorService and Executors in java.util.concurrent package. These are objects that will execute what you pass in (Runnable) in a thread. The advantage of using an Executor/ExecutorService is that you can predefine the number of threads you want to allocate and employ various strategies to grow/shrink or keep the constant.

Another thing of interest to a newbie: Threads can be daemon (background) or non-daemon (like UI) threads. Your JVM will not die if there are non-daemon threads running. To declare a Thread to be daemon, simply call setDaemon(). With an executor, you will need to provide a ThreadFactory wherein you create the thread and mark it as daemon.

Hope this helps.

kabram