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
7
votes
1 answer

Without AsyncTask, running a thread in background and updating the UI Thread

I was trying to update the recycler view content from a background thread in Kotlin. I am not using AsyncTask. Here is my code, i want to know if there is any better way than this: In my MainActivity, i have progressThread as a member variable. var…
Samrat
  • 578
  • 1
  • 5
  • 20
7
votes
2 answers

Which thread does Runnable run on?

I want to update UI every 100ms. After searching in StackOverflow, I found a solution using Runnable and Handler like this final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { …
Qk Lahpita
  • 427
  • 2
  • 10
  • 18
7
votes
2 answers

Is this thread safe?

I am writing an application for Android and am using worker threads to process certain information. Having read through my code I am now unsure if it is thread safe. I have written a simplified version of my code, I have omitted the Handler object…
Leo
  • 537
  • 1
  • 7
  • 16
7
votes
5 answers

How can we convert a callable into a runnable

I have a class which implements the callable interface. I want to schedule a task for the class using scheduleAtFixedRate method of ScheduledExecutorService interface. However scheduleAtFixedRate needs a runnable object as a command which it can…
7
votes
4 answers

Is this Runnable safe from memory leak?

I am a total beginner in Java and have created a simple Java Android snippet where in a Runnable after 1,5 seconds I change the TextView from Hello World to Hola Mundo. It works flawlessly, basically a WeakReference should prevent this memory leak…
Alper Turan
  • 1,220
  • 11
  • 24
7
votes
6 answers

Why is run() not immediately called when start() called on a thread object in java

Or is it? I have a thread object from: Thread myThread = new Thread(pObject); Where pObject is an object of a class implementing the Runnable interface and then I have the start method called on the thread object like so: myThread.start(); Now,…
pi.
  • 1,441
  • 3
  • 19
  • 25
7
votes
1 answer

JavaFX thread crashes

I have a JavaFX application that animates robots (black dots) on the screen and paints a little white line on a lighgray background at wherever they've been (think Tron). To do this I keep all the coordinates of the robots and of all the white…
Midnight
  • 373
  • 2
  • 11
7
votes
1 answer

Android: Which thread calls .onSensorChanged?

I've read a few discussions about which thread calls various callback methods, for example those associated with Sensors. Most claim that the UI thread calls the callbacks - even when a separate worker thread is involved. Are we CERTAIN about…
AndroidNewbie
  • 515
  • 5
  • 17
7
votes
3 answers

How to execute a piece of code only after all threads are done

I have a logging code which needs to be executed after all Threadss are executed. Thread t1 = new MyThread(); Thread t2 = new MyThread(); t1.run(); t2.run(); doLogging(); Is there any way to execute doLogging() only after both threads are done…
shafi
  • 801
  • 1
  • 7
  • 6
7
votes
2 answers

Passing parameter to Java Thread

Thread t = new Thread(new Runnable() { public void run() {} }); I'd like to create a thread this way. How can I pass parameters to the run method if possible at all? Edit: To make my problem specific, consider the following code segment: for (int…
Terry Li
  • 16,870
  • 30
  • 89
  • 134
7
votes
2 answers

Access Thread from inside Runnable

How can I access Thread object that is executing given Runnable? What I'd like to do is to call sleep() from within run() method.
alex
  • 10,900
  • 15
  • 70
  • 100
7
votes
3 answers

Can you have two activities running at the same time?

What I have is an activity with a severer, and another activity with different information on it. However when I open the non-sever activity the sever closes. Is there a way that I can get this to stop? If you need to see any code I would be happy…
Michael Zeuner
  • 1,736
  • 3
  • 15
  • 23
7
votes
2 answers

Use of creating a Thread by extending a Thread class

Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” Java provides two options to create a Thread class i.e either by implementing Runnable or by extending Thread class. I know there can be many reasons to implement a Runnable but…
Satya
  • 2,094
  • 6
  • 37
  • 60
7
votes
3 answers

Java : What happens if a Runnable that is being used in a thread is set to null?

say I do the following... //MyRunnable is a class that I have declared, which implements Runnable. MyRunnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); r = null; What are the implications of setting r to null like I have in the…
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
6
votes
2 answers

runOnUiThread(new Runnable() { punctuation (token) issue

Somehow it doesn't work, according to me it should be this: public void Splash(){ Timer timer= new Timer(); timer.schedule(new TimerTask(){ MexGame.this.runOnUiThread(new Runnable() { public void run(){ …
Diego
  • 4,011
  • 10
  • 50
  • 76