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

Quartz job Vs. Thread for immediate one time task

Let's say I have some unit of work that needs to get done and I want to do it asynchronously relative to the rest of my application because it can take a long time e.g. 10 seconds to 2 minutes. To accomplish this I'm considering two options:…
David
  • 14,569
  • 34
  • 78
  • 107
9
votes
3 answers

return a value after Activity.runOnUiThread() method

Is it possible to return a value after Activity.runOnUiThread() method. runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub int var = SOMETHING; // how to return var…
S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
9
votes
5 answers

SurfaceView shows black screen - Android

Basically I want to use SurfaceView for animation. Therefore the class implements Runnable. To experiment, I want to draw a circle. However, it shows only a black screen. I have been trying for days. Really appreciate if someone can…
Wallyfull
  • 181
  • 1
  • 2
  • 16
9
votes
3 answers

Check if Android handler has callbacks

I have some code which sets a timer, but if a user sets the timer while it is in use I need to remove the runnable which runs the timer and start it again. But when no handler callback runnable exists and this code is called it crashes my…
deucalion0
  • 2,422
  • 9
  • 55
  • 99
9
votes
1 answer

How to put a runnable into bundle?

I want to pass a Runnable into an activity via bundle, so that runnable must run when onCreate fires. I wrote a class which implements Serializable but it causes exception: "java.lang.RuntimeException: Parcelable encountered IOException writing…
Hossein POURAKBAR
  • 1,073
  • 2
  • 15
  • 33
8
votes
3 answers

Why is posting & cancelled a runnable on a View and Handler result in different bahviour?

I've been playing about with Runnables and have discovered that if you postDelayed a Runnable on a View then removing the callback won't work, however if you do the same but post the Runnable on a Handler then removing the callback does work. Why…
Martyn
  • 16,432
  • 24
  • 71
  • 104
8
votes
1 answer

Communication between UI thread and other threads using handler

How to do inter thread communication between UI thread and background thread? I want use common handler concept here to update my UI. I have the concept as below new Thread(new Runnable() { public void run() { while…
gd1
  • 655
  • 1
  • 10
  • 21
8
votes
4 answers

Attempt to read from field 'android.view.View android.support.v7.widget.RecyclerView$ViewHolder.itemView'

I am trying to select recyclerview item randomly with delay.I need to start random selection method after fragment load without any user interaction,But getting the following error. Afterward I put it on ImageView click to check but again I am…
Saira Nawaz
  • 710
  • 2
  • 10
  • 24
8
votes
5 answers

Where to use callable and where to use Runnable Interface?

I am quite new to Java, I was going through concepts of Multithreading, while going through various implementation of using multithreading, I went through these two concepts. This The difference between the Runnable and Callable interfaces in Java…
Geek_To_Learn
  • 1,816
  • 5
  • 28
  • 48
8
votes
4 answers

java command pattern example with Runnable class : Is Receiver missing?

From Examples of GoF Design Patterns in Java's core libraries question, it was quoted that All implementations of java.lang.Runnable are examples of Command pattern. As per my understanding of Command pattern, Client calls Invoker => Invoker…
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
8
votes
1 answer

Java 8 Function vs Consumer

I can't for the life of me find an explanation of the following: public static void takesAFunction(Function func) { func.apply("Hi I'm running a function"); } public static void takesAConsumer(Consumer func) { …
RVP
  • 2,330
  • 4
  • 23
  • 34
8
votes
3 answers

Android: "Auto refresh" after a certain time

i have search how i can do an "Auto refresh" or a runnable method for my program, i saw some posts about handlers and threads... I think that what im search from is a thread but i cant get the program to work... Let me show you some…
João Silva
  • 531
  • 4
  • 21
  • 40
7
votes
1 answer

Thread not interrupting even though I'm calling thread.interrupt()

I'm learning how to use threads in Android, and to do that I've made a small application that plays a series of notes. The idea is that there is a start button and an end button and that (obviously) if you press the start button it starts playing…
Zero
  • 1,864
  • 3
  • 21
  • 39
7
votes
4 answers

Taking heavy computation off the Android UI Thread

My Android app employs a particularly big computation which keeps crashing the system because it is on the UI thread in the Activity. I have little confidence in multithreading and so I want to get some tips on how to do it correctly. This is what I…
user485498
7
votes
3 answers

How to migrate a java command pattern using runnable to PHP 7.4?

For studying purpose, I am trying to migrate this Java Command Pattern example to PHP: https://codereview.stackexchange.com/questions/52110/command-pattern-implementation As @simon commented, using method reference operator, would modernize quite a…
celsowm
  • 846
  • 9
  • 34
  • 59