What is "runnable" in Java, in layman's terms? I am an AP programming student in high school, whose assignment is to do research, or seek out from others what "runnable" is (we are just getting into OOP, and haven't touched threads yet).
-
There is only one useful line in the API docs: "The general contract of the method run is that it may take any action whatsoever." – Tom Hawtin - tackline Nov 11 '12 at 01:20
-
A much more interesting research task would have been: What is "stoppable" in Java, in layman's terms. Todays Instructors are so ... – Udo Klimaschewski Nov 11 '12 at 01:41
-
9@UdoKlimaschewski - the guy is a **high school** student, not an undergraduate or postgraduate student. This instructor's expectations are commensurate with the level of student ... IMO. However, I agree that "first ask on SO" is NOT proper research technique for someone at the OP's level. – Stephen C Nov 11 '12 at 02:43
2 Answers
A Runnable is basically a type of class (Runnable is an Interface) that can be put into a thread, describing what the thread is supposed to do.
The Runnable Interface requires of the class to implement the method run()
like so:
public class MyRunnableTask implements Runnable {
public void run() {
// do stuff here
}
}
And then use it like this:
Thread t = new Thread(new MyRunnableTask());
t.start();
If you did not have the Runnable
interface, the Thread class, which is responsible to execute your stuff in the other thread, would not have the promise to find a run()
method in your class, so you could get errors. That is why you need to implement the interface.
Advanced: Anonymous Type
Note that you do not need to define a class as usual, you can do all of that inline:
Thread t = new Thread(new Runnable() {
public void run() {
// stuff here
}
});
t.start();
This is similar to the above, only you don't create another named class.
-
83This is a great answer that disproves entirely the claim that the question "cannot be reasonably answered in its current form". – Russell Silva Sep 23 '13 at 18:40
-
1I know it had been a long time but I have a question. Thread is only run one time or just keep running over and over inside program when I start a thread? – gamo Nov 19 '14 at 03:47
-
5The thread is run until the `run` method finishes, then the Thread is closed. If you want a thread to run over and over, place a loop inside the `run` method. – opatut Nov 20 '14 at 09:22
-
2Or simply using a Java 8 lambda function: `new Thread(() -> System.out.println("hello from other thread")).start();` – Gabe Oct 16 '18 at 17:04
Runnable
is an interface defined as so:
interface Runnable {
public void run();
}
To make a class which uses it, just define the class as (public) class MyRunnable implements Runnable {
It can be used without even making a new Thread. It's basically your basic interface with a single method, run, that can be called.
If you make a new Thread with runnable as it's parameter, it will call the run method in a new Thread.
It should also be noted that Threads implement Runnable
, and that is called when the new Thread is made (in the new thread). The default implementation just calls whatever Runnable you handed in the constructor, which is why you can just do new Thread(someRunnable)
without overriding Thread's run
method.

- 7,216
- 1
- 22
- 31
-
Are you saying there is basically no difference between inheriting Thread or implementing Runnable? Is Thread when using the Runnable used as a wrapper, as well as the executor? – opatut Nov 11 '12 at 00:56
-
I'm saying there's no difference between [extending Thread and overriding run] and [creating a new instance of Thread with a runnable that has the same run method] – Alex Coleman Nov 11 '12 at 00:58
-
Yeah got that. But is there really no difference, neither performance- nor otherwise? – opatut Nov 11 '12 at 01:02
-
3@AlexColeman - there is a **big** difference. If you extend `Thread` thread pooling is difficult and if you don't implement thread pooling you wear the *significant* cost of creating new threads all the time. By contrast, just implementing `Runnable` means that you can easily use things like `ThreadPoolExecutorService` or a 3rd-party thread pool. – Stephen C Nov 11 '12 at 02:35