Questions tagged [runnable]

The Runnable interface defines a single method, run, meant to contain the code executed in the thread.

A question about Runnable almost always really is a question about Java threads: A Java program can not create any useful thread without providing a run() method for some new Runnable instance*. That was the original reason for the Runnable interface to exist, and multi-threading may still be the most common reason why Runnable instances are created.

Some beginner programmers attempt to learn about multi-threading before they have a firm understanding of what an interface is or, the difference between a variable and an instance, etc. They may ascribe special properties to the run() method when the real magic happens in a Thread object's start() method. This is apparent in questions (and, in answers) about, "what happens when you call the run() method?" An experienced Java programmer knows that run() is just like any other method: If you want to know what it does, you should ask the person who wrote it.

The Runnable interface also is used to define tasks that may be executed by a java.util.concurrent.ExecutorService (usually, a thread pool) and, like any other interface, it may be used for in-thread callbacks, or in any other way that a programmer sees fit.

The Runnable interface, just like any other interface, may be used by creating a named class that implements it:

class Foobar implements Runnable {
     public Foobar(...) { ... }

     @Override
     public void run() { doSomething(); }
}
Runnable r = new Foobar(...);

And, like any other interface or class, it may be used as the template for an anonymous inner class:

Runnable r = new Runnable() {
    public void run() { doSomething(); }
};

Finally, since Java8, Runnable is an @FunctionalInterface which means that a new instance also can be created with a lambda expression:

Runnable r = () -> doSomething(); 

Answers to questions about run() should emphasize that t.start() is the method that the library provides for your code to call when it's time to start a new thread, and run() is the method that you provide for the library to call in the new thread.

See Defining and Starting a Thread tutorial for simple examples of Runnable implementations.


*A Thread instance is a Runnable instance.

2051 questions
2348
votes
42 answers

"implements Runnable" vs "extends Thread" in Java

From what time I've spent with threads in Java, I've found these two ways to write threads: With implements Runnable: public class MyRunnable implements Runnable { public void run() { //Code } } //Started with a "new Thread(new…
user65374
  • 24,139
  • 4
  • 19
  • 7
565
votes
14 answers

The difference between the Runnable and Callable interfaces in Java

What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other?
Scottm
  • 7,004
  • 8
  • 32
  • 33
292
votes
20 answers

Naming threads and thread-pools of ExecutorService

Let's say I have an application that utilizes the Executor framework as such Executors.newSingleThreadExecutor().submit(new Runnable(){ @Override public void run(){ // do stuff } } When I run this application in the debugger, a…
mre
  • 43,520
  • 33
  • 120
  • 170
251
votes
14 answers

What's the difference between Thread start() and Runnable run()

Say we have these two Runnables: class R1 implements Runnable { public void run() { … } … } class R2 implements Runnable { public void run() { … } … } Then what's the difference between this: public static void main() { R1 r1 =…
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79
209
votes
10 answers

Runnable with a parameter?

I have a need for a "Runnable that accepts a parameter" although I know that such runnable doesn't really exist. This may point to fundamental flaw in the design of my app and/or a mental block in my tired brain, so I am hoping to find here some…
uTubeFan
  • 6,664
  • 12
  • 41
  • 65
117
votes
2 answers

In a simple to understand explanation, what is Runnable in Java?

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).
user1809295
  • 1,361
  • 2
  • 9
  • 8
93
votes
10 answers

Is there a way to make Runnable's run() throw an exception?

A method I am calling in run() in a class that implements Runnable) is designed to be throwing an exception. But the Java compiler won't let me do that and suggests that I surround it with try/catch. The problem is that by surrounding it with a…
Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
78
votes
4 answers

How to remove a runnable from a handler object added by postDelayed?

I have an "open" animation and am using Handler.postDelayed(Runnable, delay) to trigger a "close" animation after a short delay. However, during the time between open and close, there is possibly another animation triggered by a click. My question…
Bruce Lee
  • 3,049
  • 3
  • 20
  • 13
77
votes
3 answers

Lambda that does absolutely nothing

I needed to have a lambda expression of the functional interface Runnable that did nothing. I used to have a method private void doNothing(){ //Do nothing } and then use this::doNothing. But I've found an even shorter way to do this.
Rien
  • 1,596
  • 1
  • 12
  • 16
76
votes
4 answers

How to replace HashMap Values while iterating over them in Java

I am using a Runnable to automatically subtract 20 from a players cooldown every second, but I have no idea how to replace the value of a value as I iterate through it. How can I have it update the value of each key? public class CoolDownTimer…
Zach Sugano
  • 1,567
  • 5
  • 22
  • 41
69
votes
8 answers

Returning a value from Runnable

The run method of Runnable has return type void and cannot return a value. I wonder however if there is any workaround of this. I have a method like this: public class Endpoint { public method() { Runnable runcls = new RunnableClass(); …
Grzzzzzzzzzzzzz
  • 1,301
  • 4
  • 20
  • 33
63
votes
5 answers

Runnable::new vs new Runnable()

Why doesn't the first of the following examples work? run(R::new); method R.run is not called. run(new R()); method R.run is called. Both examples are compiled-able. public class ConstructorRefVsNew { public static void main(String[] args) { …
user1722245
  • 2,065
  • 1
  • 18
  • 31
61
votes
6 answers

Difference between AsyncTask and Thread/Runnable

I have question which puzzles me. Imagine I wanna do something in another thread, like fetching GPS/Location stuff, which as recommended in the SDK documents, must use a background thread. So here is the question: What's the difference between…
51
votes
4 answers

Updating GUI: Runnables vs Messages

To update the GUI from other threads, there are basically two main approaches: Use java.lang.Runnable with any of these methods: Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long) Handler.post(Runnable) Use…
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
43
votes
11 answers

Best way of creating and using an anonymous Runnable class

I want to use an anonymous class for Runnable. There are two ways, but I don't know if they do the same thing or not: Method one: using Runnable directly and then calling run(): new Runnable() { @Override public void run() { …
hqt
  • 29,632
  • 51
  • 171
  • 250
1
2 3
99 100