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
6
votes
2 answers

Why is run() getting called just once?

Consider this class, AnimationThread: class AnimationThread implements Runnable { public void pause() { doAnimation = false; } public void doStart(){ doAnimation = true; } @Override public void run() { …
padam thapa
  • 1,471
  • 2
  • 20
  • 41
6
votes
6 answers

Schedule Java task for a specified time

I would like to be able to schedule a task at a specific time in Java. I understand that the ExecutorService has the ability to schedule at periodic intervals, and after a specified delay, but I am looking more for a time of day as opposed to after…
Ray
  • 4,829
  • 4
  • 28
  • 55
6
votes
4 answers

Can a Runnable return a value?

Is it possible for a Runnable to return a value? I need to do some intensive work on an Editable and then return it back. Here is my mock code. public class myEditText extends EditText { ... private Editable workOnEditable() { final Editable…
bwoogie
  • 4,339
  • 12
  • 39
  • 72
6
votes
6 answers

Why is the java.lang.Thread class in Java not marked final by the designers?

What is the essence of allowing the user to create thread by extending the Thread class when we can achieve the same functionality by implementing Runnable and pass it to the Thread constructor.
6
votes
2 answers

How to resolve "expecting class body" error in kotlin

The code: var shouldStopLoop = false val handler = object : Handler() val runnable = object: Runnable // The error occurs here { override fun run() { getSubsData() if(!shouldStopLoop) { …
curiousgeek
  • 903
  • 11
  • 18
6
votes
1 answer

Runnable is not being executed completely after the delay is over from Handler's postDelayed() in Oppo F1

The logcat shows the following error when run application in oppo f1 version 5.1: ANR_LOG: >>> msg's executing time is too long Blocked msg = { when=-15s421ms what=0 target=android.view.Choreographer$FrameHandler…
IID
  • 93
  • 2
  • 8
6
votes
2 answers

How does Platform.runLater() function?

I have a simple app which updates the data in the background and while it updates, it disables all the other buttons and enables a TextArea to show the progress. Steps: Disable all the other buttons in the mainUI (Button name: plotButton) Enable a…
Kavyajeet Bora
  • 612
  • 1
  • 7
  • 15
6
votes
1 answer

ScheduledExecutorService execute every night at 12 AM UTC Time

I want to start ScheduledExecutorService exactly 12 AM daily ,Schedule has to Start at today at 22/02/2017 00:00:00 (UTC TIME),Can any one tell me Whether my code is Correct or not? DateTime today = new DateTime().withTimeAtStartOfDay();…
kavie
  • 2,154
  • 4
  • 28
  • 53
6
votes
1 answer

difference between post(Runnable) and sendMessage(Message) in Handler

I just want to know what is the exact difference between using sendMessage (Message msg) and post (Runnable r). Since both these methods are going to run in Main UI Thread even if we have Seperate Runnable.
saravanan
  • 5,339
  • 7
  • 45
  • 52
6
votes
1 answer

Java - Pass a method to a class and execute it in a new Thread

[Check the bottom of the question for updates] As in the title, I'd like to write a class which takes in a method and executes it in a new Thread. I lurked around SO and came up with something like: import java.util.concurrent.Callable; public…
Tom
  • 92
  • 13
6
votes
1 answer

Java - super keyword in new thread Runnable - refers non static method thru class

In all the super keyword tutorials I found online, it's hard to get any examples closer to following one. My question: What's the difference between Tracker.super.track(event); and test.parent.Tracker.track(event); ? Why would the first work?…
Weishi Z
  • 1,629
  • 5
  • 18
  • 38
6
votes
1 answer

FacesContext.getCurrentInstance() returns null in Runnable class

I am trying to get the FacesContext by calling FacesContext.getCurrentInstance() in the run() method of a Runnable class, but it returns null. public class Task implements Runnable { @Override public void run() { FacesContext…
Elena
  • 145
  • 4
  • 13
6
votes
0 answers

why does ExecutorService not have invokeAll for Runnable tasks

I noticed that there are submit methods for both Runnable task and Callable task. Why is there only invokeAll for Callable tasks, but no invokeAll for Runnable tasks. Thanks
Jacky
  • 8,619
  • 7
  • 36
  • 40
6
votes
2 answers

Playback video in slow motion in android

- I am working on a project which needs to play video in slow motion. - I am well aware that Android doesn't provide these functionality. - I found PVPlayer Engine and libVLC which possessed these capabilities, but i didn't found any tutorial or…
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
6
votes
3 answers

Execute all scheduled (postDelayed) runnables in Handler

I use a Handler, which post some Runnable via postDelayed(r, DELAY_TIME), but I need to execute all Runnables before posting new via postDelayed. Any good ideas how to achieve this as simple as possible? Edit: I want it basically like…
Leandros
  • 16,805
  • 9
  • 69
  • 108