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

Java asserts not send to console when called from a runnable

In my program I made an assert - which evaluates to false - from a runnable, but never see any console output about the assert. I want to know if my asserts are false, but it seems the runnable is catching all the thrown asserts? Below is the…
Dijkstra
  • 323
  • 1
  • 9
6
votes
4 answers

How do I use wait/notifyAll

I am wondering if it is possible in java to have a class that implements Runnable and if an object of the class goes to wait() (so thread stops running until it receives a signal), can another object of the same class notify it to continue running…
user597608
  • 387
  • 8
  • 20
6
votes
5 answers

Java: How do I catch InterruptedException on a thread, when interrupted by another thread?

I'm developing a multithreaded application to make connections to external servers - each on separate threads - and will be blocked until there is input. Each of these extends the Thread class. For the sake of explanation, let's call these…
ikevin8me
  • 4,253
  • 5
  • 44
  • 84
6
votes
3 answers

How to start/stop Runnable/Handler?

I'm trying to maintain databases synchronized between a Webservice and Android app. The code below is working, but I encounter some problems: Every time I go to main page of App a new infinite process is started. The process never ends Can anyone…
Zartch
  • 126
  • 1
  • 1
  • 7
6
votes
5 answers

My JProgressBar is not Updating Until it is 100%

Ok, I have the following code. public class MyProgressBar extends JPanel implements MyData, Serializable { /** * */ public static final int MAX = 10000; public static final int WIDTH = 400; …
Patrick Aquilone
  • 584
  • 2
  • 11
  • 28
6
votes
2 answers

How to start a background thread that doesn't block the main thread in Java?

I have the following Java code: public static void main(String[] args) { new Thread(new MyRunnable()).run(); showGUI(); } My problem is that starting MyRunnable blocks the main thread, causing showGUI to not be called until it finishes…
XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151
5
votes
1 answer

How do you loop a thread?

I have a thread containing a runnable. I need this to loop infinitely unless cancelled by the user. I have no idea how to go about this. All help is greatly appreciated. Cheers.
Alex Haycock
  • 375
  • 2
  • 3
  • 12
5
votes
2 answers

Creating Jar file - doesn't work on other computers

I'm trying to package my program into a JAR file so it can be used on multiple computers. My program is composed of start.java, userinterface.java and writer.java. The program, written in Eclipse, works perfectly on my computer. When exported, it…
user1147964
  • 143
  • 2
  • 11
5
votes
2 answers

Does handler.post(runnable) start a new thread?

If the handler was instantiated in the main UI thread, does a post with a Runnable create a child thread that gets added to the message queue, or does it just get run in the UI thread? handler.post(new Runnable(){ public void run() { //…
xil3
  • 16,305
  • 8
  • 63
  • 97
5
votes
3 answers

Updating UI from a service (using a handler?)

I am trying to update my UI in FirstActivity when I receive a notification but is confused by runOnUiThread , Runnable and Handler. Here is what I have: I am running FirstActivity and NotificationService. When NotificationService reeives a…
newbie
  • 958
  • 4
  • 13
  • 25
5
votes
7 answers

How to keep an object parameter unchanged in a Runnable Class in Java?

I have a Runnable class like: Class R1 implements Runnable { private static final Log LOGGER = LogFactory.getLog(R1.class); private final ObjectClass obj; private final SomeService service; public R1(ObjectClass obj, SomeService service) { …
newguy
  • 5,668
  • 12
  • 55
  • 95
5
votes
5 answers

How to implement a queue of runnables

I am trying to implement a queue of runnables to be executed one after another(meaning the next in the queue will execute after the other has completed) during an asynchronous task. I wrote a Manager to manage these runnables and task that is a…
ninjasense
  • 13,756
  • 19
  • 75
  • 92
5
votes
2 answers

How do I pass Runnable objects to a Handler?

I am learning via a book and it gives me this example: Handler handler=new Handler() { @Override public void handleMessage(Message msg) { bar.incrementProgressBy(5); } }; and Thread background=new Thread(new Runnable() {…
Ryan
  • 9,821
  • 22
  • 66
  • 101
5
votes
3 answers

how can i know how a task finished when using java's Future class

I'm using the java's Future class to execute a task, but the method isDone returns true if the task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true. is…
user695907
  • 61
  • 1
  • 4
5
votes
3 answers

Valid void Return Statements from Lambda Expressions (Example: Runnable)

Seeing some stranger behavior in Java regarding functional interfaces with void return type. Can someone please explain why the declarations for task5 and task6 below compile? public class Test { private static int counter; private static…
John Fisher
  • 305
  • 1
  • 13