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
18
votes
5 answers

Implicit conversion to Runnable?

As an exercise, I tried to create an implicit conversion that would accept a function and produce a Runnable. That way you could call Java methods that accept Runnable objects and use them like closures. The implicit conversion is easy enough: …
Patrick Arnesen
  • 1,138
  • 1
  • 9
  • 13
18
votes
2 answers

Starting a runnable in background thread

I have, to my knowledge, implemented a runnable which is created on a new thread. However, the thread does not seem to be running in the background, and the actions taken inside the runnable are stalling the UI with heavy actions. See…
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
17
votes
2 answers

Android: one handler for all runnables?

Can I use one handler in my Activity for all runnables or should I have multiple instances of Handler, each for one runnable?
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
17
votes
3 answers

How-to use Runnable in Mono for android

I'm try to lern Monodroid! I try to re-write java code to C# and have some problem: I don't understand how-to use Runnable. That's snipet of code in Java, that I coudn't translate to C#: public class RunActivity extends Activity implements…
Beyka
  • 1,372
  • 1
  • 10
  • 15
15
votes
5 answers

Explain what the following code does?

java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); Please tell me what does the above code does actually. I am looking for line by line explanation. especially the first line…
Deepak
  • 6,684
  • 18
  • 69
  • 121
15
votes
3 answers

Return value by lambda in Java

Till now I manage to find all answers I need but this one confusing me. Let's say we have example code: public class Animal { private String species; private boolean canHop; private boolean canSwim; public Animal(String speciesName,…
Freeman
  • 232
  • 1
  • 2
  • 11
15
votes
5 answers

How to stop Handler in Android

In my application I have created a calendar with Gridview and in that Gridview I am displaying dates and some availability of events with the help of Imageview and to do this I have created a handler. Now I want to stop the…
Abhishek Dhiman
  • 1,631
  • 6
  • 25
  • 38
14
votes
2 answers

Does the future object returned by executorService.submit(Runnable) hold any reference to the runnable object?

Let's assume we have the following code: List> runningTasks; ExecutorService executor; ... void executeTask(Runnable task){ runningTasks.add(executor.submit(task)); } My questions are: Does runningTasks hold a reference to the task…
Daniel Rusev
  • 1,331
  • 2
  • 16
  • 35
14
votes
2 answers

Bad practice to use Runnable as callback / subroutine?

Is it's considered bad practice to use Runnable as a callback? Considering that Runnable is meant to be used with threads (see it's JavaDoc), I'm wondering if this is okay - or whether I should make my own interface for this purpose. What I'm…
MightyPork
  • 18,270
  • 10
  • 79
  • 133
14
votes
3 answers

Is there already a StopWatch class for android and why doesn't my implementation work?

Lately I saw http://developer.android.com/reference/android/os/CountDownTimer.html and wondered if there is an respective class for stop watches since I want to tell the user of my App how long he's already trying to solve the puzzle. I mean it…
principal-ideal-domain
  • 3,998
  • 8
  • 36
  • 73
13
votes
3 answers

LiveData and ViewModel does not update textview

I am having a small issue, when i comment out the binding.setData(dataContainer); in onChanged while observing Livedata in my Activity, I am not able to update the UI, when I uncomment it, the UI is updated. Please guide and do a little code review…
13
votes
1 answer

How assign a method reference value to Runnable

I hava a problem about Java 8 Runnable. public static void main(String[] args) { Runnable r1 = Test::t1; Runnable r2 = Test::t2; Runnable r3 = Test::t3; } public static void t1() { } public static String t2() { return…
yi jiang
  • 216
  • 1
  • 13
13
votes
2 answers

Lambda casting rules

I was curious why a lambda with a return type can not be casted to a Runnable whereas a non void method reference can. Runnable r1 = () -> 1; // not allowed // error: incompatible types: bad return type in lambda expression // int cannot be…
flakes
  • 21,558
  • 8
  • 41
  • 88
12
votes
2 answers

Java stop executor service once one of his assigned tasks fails for any reason

I need some kind of service that will run a few tasks simultaneously and in an interval of 1 second for 1 minute. If one of the tasks fails, I want to stop the service and every task that ran with it with some kind of indicator that something went…
totothegreat
  • 1,633
  • 4
  • 27
  • 59
12
votes
1 answer

How do I change the rate or period of a repeating task using ScheduledExecutorService?

I have a modified version of the bluetooth chat sample app. I have set up a ScheduledExecutorService which sends a command over bluetooth at a predefined rate using scheduleAtFixedRate. I have set up a PreferenceActivity to allow the period to be…