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

@PreDestroy not being called for a Runnable

I have a spring context in which a we have Runnable beans started like so: public void startChildAndWait(Class type) { BaseMyDispatcher child = appContext.getBean(type); child.initialize(this); //The child references its parent during run…
Alex Sayegh
  • 233
  • 4
  • 16
0
votes
1 answer

Runnable not working properly

public void delay() { //give delay before comp can play Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { startPlay(); } }, 1500); …
JVamcas
  • 122
  • 3
  • 12
0
votes
1 answer

vaadin runWhileAttached method

How do you implement the runWhileAttached method, like in this demo: http://demo.vaadin.com/charts/#SplineUpdatingEachSecond? runWhileAttached(chart, new Runnable() { @Override public void run() { final long x =…
matcha
  • 81
  • 1
  • 9
0
votes
1 answer

Handler postDelayed runnable how to know time till start?

I have a Handler and a runnable to be done in 3000ms when I touch the screen. How can I display the time left to happen since I touched the screen? Is there any way to display it on a textview or something alike?
Deldri
  • 37
  • 4
0
votes
2 answers

Meassure 1000 miliseconds most accurately (update every 10 ms)

I am trying to create an app which sends sms. As confirmation I requires user to hold down a "Send" button for one second. I am showing a progress bar which acts like countdown timer for them. I wanted to update progress of progress bar every 10…
horin
  • 1,664
  • 6
  • 23
  • 52
0
votes
1 answer

Android remove runnable callbacks

I am designing a bonus runnable such that for each question, if the user can complete the question earlier, there is a bonus score for time remaining for each question. Code: private Runnable Run_bonus_countdownHandler = new Runnable() { …
pearmak
  • 4,979
  • 15
  • 64
  • 122
0
votes
1 answer

JavaFX Multithreading

I am facing one problem in my JavaFx application. Preface: I don't want to be specific to my application only but want to be generalized so that people like me will get an idea on similar situation. Background: Implementing a Javafx application…
Nepal12
  • 583
  • 1
  • 12
  • 29
0
votes
1 answer

How to stop handler.postDelayed in for-loop?

I saw no real answer for this type of using "handler.posDelayed". So, I execute "handler.posDelayed" multiple times with a for-loop. The problem is, when I leave the activity and restart it, the new objects AND the old ones from the handler are…
Jeff
  • 27
  • 2
  • 9
0
votes
2 answers

Schedule to run a method at periodic time´

I have to schedule a method to be executed when starting and periodically thereafter at intervals of 1 minute. For that I have done this: public void init(){ loadConfig(); //method which needs to be executed periodically Timer scheduler =…
aayush_v20
  • 193
  • 1
  • 9
  • 22
0
votes
1 answer

ExecutorService fairness

I have an ExecutorService like this ExecutorService executor = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000, true)); and i'm sending work to it with .execute(Runnable) My…
AdrianS
  • 1,980
  • 7
  • 33
  • 51
0
votes
0 answers

Passing an Argument in a periodic runnable task execution using handler in Android

I have created a periodic execution of runnable task by using handler in android, but I want to pass an argument in that periodic task execution, I have tried 2 approaches to pass an argument, 1 - By declaring a class in the method void Foo(String…
0
votes
1 answer

No response from second client socket

I am working with Java sockets. I have a server socket and two client sockets. My problem is, that the first client socket submits its message to my server socket, and the message from the second client socket is not arriving at the server socket.…
KJaeg
  • 698
  • 3
  • 7
  • 23
0
votes
2 answers

Dining philosophers - I spoke to all, just one listens

I'm implementing dining philosophers problem and I faced a problem myself, I do not know what's the cause, hence I am here. It is after dinner when I'm telling them to leave, I want to force them to create reports cause that's the next stage of…
Suspended
  • 1,184
  • 2
  • 12
  • 28
0
votes
2 answers

Handler postdelayed is not fired after given delay interval when quitting the app in android?

Code Snippet: Handler handler= new Handler(); handler.postDelayed(networkRunnable, 10000); /** * A runnable will be called after the 10 second interval */ Runnable networkRunnable= new Runnable() { @Override public void…
M Vignesh
  • 1,586
  • 2
  • 18
  • 55
0
votes
1 answer

Retrieve JSON Object from a jar file

I have created a runnable jar file with a JSON Object "J" as return type in a method A() of Class A. JSON Obj was in a package a.b. Now my question is that, When I import the jar in any other project the return type JSON obj needed to be placed in…
Himaja
  • 1
  • 1
  • 1
1 2 3
99
100