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
12
votes
4 answers

Why use Thread.currentThread().isInterrupted() instead of Thread.interrupted() when implementing Runnable?

On stackoverflow, I often see the use of Thread.currentThread().isInterrupted(). When implementing Runnable and using it in a while loop, like so: public void run() { while(!Thread.currentThread().isInterrupted()) { ... } } is there any…
Christian
  • 6,070
  • 11
  • 53
  • 103
12
votes
3 answers

How to stop runnable when the app goes to background?

I am trying to establish a runnable which can load ads by every 5 sec interval (of course 5 sec is too fast, it's just for testing purpose) Here is my code: package com.admobsdk_dfp_handler; import com.google.ads.*; import…
Kit Ng
  • 993
  • 4
  • 12
  • 24
11
votes
5 answers

Scenario of extending Thread class and implementing Runnable interface

I am new to thread programming in Java and hence this basic question. (I checked, but could not find this question previously asked) I read that threads can be created either by inheriting the Thread class or by implementing the Runnable…
noMAD
  • 7,744
  • 19
  • 56
  • 94
11
votes
3 answers

Getting a result in the future?

I'm looking to get a result from a method which can take a while to complete and doesn't actually return the object, so I'd like to deal with it as effectively as possible. Here's an example of what I'm trying to achieve: public static void main…
Anonomoose
  • 137
  • 1
  • 1
  • 8
11
votes
5 answers

Scenario where extending thread is preferred than implement Runnable?

As a beginner, I was reading about 2 ways of implementing Multithreading in Java. I read this thread on SO and on many other threads. It is stated that "prefer runnable" , extends thread only when you are specialising Thread's behaviour. Can…
Raj
  • 692
  • 2
  • 9
  • 23
11
votes
3 answers

Why do variables passed to runnable need to be final?

If I have a variable int x = 1, say, and I declare a runnable in the main thread, and I want to pass x to the runnable's run() method, it must be declared final. Why? final int x = 0;//<----must be final... private class myRun implements Runnable…
user485498
10
votes
4 answers

Kotlin - How to pass a Runnable as this in Handler

I'm beginner in kotlin. I try to create a task that will repeat every 2 seconds. So I created something like this. val handler = Handler() handler.postDelayed(Runnable { // TODO - Here is my logic // Repeat again after 2…
Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
10
votes
1 answer

What is the Runnable representation in java.util.function?

I was wondering recently as Runnable is very often used in a functional context. At the same time according to javadoc its semantical meaning is very close to multithreading while it is not aways used in such context: The Runnable interface should…
Alexander Petrov
  • 9,204
  • 31
  • 70
10
votes
5 answers

Context inside a Runnable

I try to play a sound from R.raw. inside a Thread/Runnable But I can't get this to work. new Runnable(){ public void run() { //this is giving me a NullPointerException, because getBaseContext is null MediaPlayer mp =…
oggy
  • 101
  • 1
  • 1
  • 4
10
votes
1 answer

android intent chooser slow to appear

When users have a lot of possible apps that can accept a certain kind of intent, the intentchooser takes a while to load, a noticeable amount of time at least Yet this waiting happens after I launch intent, is there any way I can put up some kind of…
CQM
  • 42,592
  • 75
  • 224
  • 366
10
votes
3 answers

Androids Handler.post, what happens exactly

since several days, I tried to figure out what exactly happens if I execute code in void function(){ //somePreExecutionCode new Handler().post(new Runnable(){ @Override public void run(){ //someCode } }); } It seems like…
Ef Ge
  • 788
  • 1
  • 9
  • 26
9
votes
2 answers

Android update ui from handler every second

I need a little help with updating my UI from Runnable/Handler every second. I'm using this code : Runnable runnable = new Runnable() { @Override public void run() { handler.post(new Runnable() { …
Android-Droid
  • 14,365
  • 41
  • 114
  • 185
9
votes
1 answer

ScheduledThreadPoolExecutor, how stop runnable class JAVA

I have written the following code: import java.util.Calendar; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; class Voter { public static void main(String[] args) { ScheduledThreadPoolExecutor…
Khozzy
  • 1,064
  • 4
  • 15
  • 29
9
votes
1 answer

"Runnable::run" - How is this creating an Executor instance?

I'm working on a project where the following line is used to create a test Executor member variable instance: private Executor executor = Runnable::run; The code runs and compiles but I don't understand how Runnable::run creates an instance of…
T.R.
  • 171
  • 8
9
votes
3 answers

Handler post is not working in Kotlin Android

Could someone show me what is wrong? I try to use a Handler post a Runnable but it's not execute var mHandler: Handler? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) …
songoku1610
  • 1,576
  • 1
  • 13
  • 17