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

Android: How do I stop Runnable?

I tried this way: private Runnable changeColor = new Runnable() { private boolean killMe=false; public void run() { //some work if(!killMe) color_changer.postDelayed(changeColor, 150); } public void kill(){ …
c0dehunter
  • 6,412
  • 16
  • 77
  • 139
40
votes
5 answers

removeCallbacks not stopping runnable

I am calling from a method: myHandler.postDelayed(mMyRunnableHide, 6000); which calls: public Runnable mMyRunnableHide = new Runnable() { public void run() { mTextDisplay.setText(""); DisplayX(); } }; if a button on…
codemanusa
  • 579
  • 2
  • 5
  • 7
37
votes
5 answers

Is there a way to pass parameters to a Runnable?

I have a thread that uses a handler to post a runnable instance. it works nicely but I'm curious as to how I would pass params in to be used in the Runnable instance? Maybe I'm just not understanding how this feature works. To pre-empt a "why do…
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
37
votes
6 answers

Should you synchronize the run method? Why or why not?

I have always thought that synchronizing the run method in a java class which implements Runnable is redundant. I am trying to figure out why people do this: public class ThreadedClass implements Runnable{ //other stuff public synchronized…
MHP
  • 385
  • 1
  • 4
  • 8
36
votes
4 answers

Android update TextView in Thread and Runnable

I want to make a simple timer in Android that updates a TextView every second. It simply counts seconds like in Minesweeper. The problem is when i ignore the tvTime.setText(...) (make it //tvTime.setText(...), in LogCat will be printed the following…
user1718339
  • 373
  • 1
  • 3
  • 7
35
votes
2 answers

Why does Thread implement Runnable?

A Java Thread's run() method is called by the JVM, on that thread, when the thread starts. To give a thread something to do, you can make a subclass of Thread and override its run() method, or (preferred) you can supply a Runnable to the thread's…
Boann
  • 48,794
  • 16
  • 117
  • 146
34
votes
5 answers

Why "implements Runnable" is Preferred over "extends Thread"?

The Java Thread itself implements a Java Runnable! and according to most of the experts over Internet, implements Runnable is preferred over extends Thread! even though we cannot use utilize Runnable in the sense of thread with out the Thread…
Tariq
  • 2,489
  • 11
  • 36
  • 61
26
votes
6 answers

Runnable is posted successfully but not run

In an existing Android project I've encountered the following piece of code (where I inserted the debugging litter) ImageView img = null; public void onCreate(...) { img = (ImageView)findViewById(R.id.image); new Thread() { public…
mvds
  • 45,755
  • 8
  • 102
  • 111
24
votes
2 answers

how can I pass a variable into a new Runnable declaration?

I have the following : Runnable done = new Runnable() { public void run() { System.out.println("Hello"); } }; And then in my Android activity I'll call something like : runOnUIThread(done); Which I then…
Jimmy
  • 16,123
  • 39
  • 133
  • 213
23
votes
3 answers

Calling @Transactional methods from another thread (Runnable)

Is there any simple solution to save data into database using JPA in a new thread? My Spring based web application allows user to manage scheduled tasks. On runtime he can create and start new instances of predefined tasks. I am using spring's…
Ondřej Míchal
  • 573
  • 2
  • 5
  • 14
21
votes
8 answers

new Runnable() but no new thread?

I'm trying to understand the code here , specifically the anonymous class private Runnable mUpdateTimeTask = new Runnable() { public void run() { final long start = mStartTime; long millis = SystemClock.uptimeMillis() - start; int seconds =…
sgarg
  • 2,340
  • 5
  • 30
  • 42
21
votes
7 answers

How does one implement a truly asynchronous java thread

I have a function that needs to perfom two operations, one which finishes fast and one which takes a long time to run. I want to be able to delegate the long running operation to a thread and I dont care when the thread finishes, but the threads…
Ritesh M Nayak
  • 8,001
  • 14
  • 49
  • 78
19
votes
5 answers

Handler to run task every 5 seconds Kotlin

I would like to run a certain code every 5 seconds. I am having trouble achieving this with a handler. How can this be done in Kotlin? Here is what I have so far. Also to note, the variable Timer_Preview is a Handler.
Ryan Dailey
  • 286
  • 1
  • 6
  • 13
19
votes
2 answers

Why does 'extends Thread' exist, when 'implements Runnable' is winner in all cases

I know that implements Runnable is preferred over extends Thread in Java threads as it allows us to extend some other class if it is required. But if this is the case, does extends Thread also have its own advantages over implements Runnable and if…
rahul
  • 1,095
  • 8
  • 22
18
votes
1 answer

Android Asynctask vs Runnable vs timertask vs Service

What are the differences between these methods (classes)? I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a…
shafqat
  • 183
  • 1
  • 2
  • 5