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

Exporting Java project as runnable Jar: not doing anything

So I have a problem with exporting my eclipse project. The project works perfectly when running it inside eclipse, but when exporting it, it doesn't do anything. I can't post a third link since I have less than 10 reputation, but in there I chose…
aze45sq6d
  • 876
  • 3
  • 11
  • 26
0
votes
0 answers

Appending JTextArea from a thread?

I am trying to append a text area in my gui class, with information from another controller class that is running a thread(s). I am a bit confused by threads in general, and am confused as to why this does not work. Here is the trace: Exception in…
UnMonk
  • 1
  • 1
0
votes
1 answer

Java ExecutorService Runnable doesn't update value

I'm using Java to download HTML contents of websites whose URLs are stored in a database. I'd like to put their HTML into database, too. I'm using Jsoup for this purpose: public String downloadHTML(String byLink) { String htmlInPage = ""; …
0
votes
1 answer

How can I be sure that my android runnable started on a worker thread?

I am trying to learn about doing background tasks on Android/Java by using threading. I made a simple test application which purely did a new Thread(new Runnable()) and did sleeps inside the runnable. When this was running, I was still able to click…
0
votes
1 answer

Updating the Listview data from remote servers

Hello I am trying to update my listview data on an activity from remote servers(php and mysql), I am using the Async task method to call the data from the servers, but I am calling the async task method every 2 seconds. This is how I do it /** …
George
  • 1,086
  • 14
  • 48
0
votes
1 answer

How can I export as Runnable JAR file from eclipse?

When I try to export as a Runnable JAR file, I have no options for 'Launch configuration'. Why is this? I am new to programming, can anyone help me? It's a code for the game Snake. Do I need a main()? import java.awt.Canvas; import…
AClark
  • 11
  • 1
0
votes
1 answer

How to get current location through Android GPS using Runnables and searching TimeOut?

I'm trying to develop some solution to get my current location using Android's GPS. However, the search is limited to a timeout and also when search locations begins there is a Dialog which allows user to cancel the search. To do this, I'm thinking…
0
votes
1 answer

Run method from extends Activity extends Runnable

I'm trying to call a method from inside a Runnable that is running. It waits for a string to be entered and when it is then depending on the string (the strings act as commands) it calls a method and is supposed to run whats inside it. public class…
0
votes
0 answers

Couldnt find ab-cd config file for this filepath:config.properties. package shade

mvn package - ruunable jar file is unable to locate this properties file. In Eclipse my application runs fine but after i create mvn package. runnable jar file is giving me me null pointer. I used 7zip to view the create runnable jar file i see…
Arpitha Mamidala
  • 175
  • 1
  • 2
  • 8
0
votes
0 answers

how to start or call an onCreate in android in the same way or manner of VB6's Form_Load

my android app has let's say activity A and B. what i need is to start my activity B at startup without showing it and of course show activity A because it is the main activity. here's my MainActivity code: public class MainActivity extends Activity…
halfBlue3
  • 13
  • 8
0
votes
0 answers

start an activity without showing it at start up

I've several question that is like my question but it is too complicated for me and my dilemma. what i just want is to start my second activity without it showing at start up. i used this code: startActivity(new Intent(MainActivity.this,…
halfBlue3
  • 13
  • 8
0
votes
2 answers

Runnable, Thread, RunOnUIThread

Today I managed to speed-up my Android application a lot by using a Runnable handling a piece of code inserting/updating and deleting some database stuff. However I also used RunOnUiThread() and a normal Thread() but I don't have any idea what the…
user443346
0
votes
0 answers

android handler delay for loop

I'd like to use handler.postDelayed instead of Thread.sleep() in a loop. Basically I have a sequence of numbers in a string and for each number I need a different delay to do an specific action. hThread = new HandlerThread("HandlerThread"); …
0
votes
2 answers

Get onActivityResult not on the same class send startActivityForResult

I try to make the speech recognize as show in this link https://software.intel.com/en-us/articles/developing-android-applications-with-voice-recognition-features. It uses a run() method to make it thread on the background, which I call it from the…
0
votes
1 answer

Why the runnable stops in my widget?

I have the following code that updates me on a TextView the total data traffic in MB. At first when you load the widget on the screen runs but then stops. I checked the Runnable with Log.d and it stops running. Why?. Can you help me? public class…
Garmael
  • 422
  • 3
  • 7
  • 19