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

Double runnable running in background

I am currently polishing my apps offline features. Before those offline features, if I remember correctly, works as I what and of course as it should be. There is a feature where my app calculates or estimates the bill per month of a household and…
halfBlue3
  • 13
  • 8
0
votes
1 answer

Google's Direct WiFi Demo remove discovery button

I need to use for my app WiFi Direct and I found Google's Demo but it needs many, many changes. The first problem that I have encounter is that I cannot make the app auto scan for devices because there are fragments all over the place and every time…
We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46
0
votes
1 answer

NoClassDefFoundError on implementing Runnable class in android studio

I keep getting NoClassDefFoundError on my code. Here is the code: handler.postDelayed(check = new Runnable() { @Override public void run() { if (foreground && paused) { foreground = false; …
0
votes
1 answer

Android update UI every 2 seconds

I have been searching around and tried out solutions for almost a day but nothing worked, so I decide to post my code here and hope someone can give me some help, thanks! I have a fragment which extends ListFragment. The list has four items; For the…
Shawn Li
  • 99
  • 2
  • 13
0
votes
1 answer

ScheduledExecutorService - Ignore already running runnable

I'm using a scheduled executor service private ScheduledExecutorService pool = new ScheduledThreadPoolExecutor(1); running a runnable at fixed rate pool.scheduleAtFixedRate(new CoolRunnable(), 10, 10, TimeUnit.MILLISECONDS); This thread pool…
Jofkos
  • 739
  • 1
  • 9
  • 27
0
votes
2 answers

Take action for every two minutes of inactivity (no user interaction)

I am trying to write to a file whenever the user has not interacted with the application for 2 minutes. Currently am having base activity in which I have overriden the onUserInteraction method. In this method the float time variable is reset and…
android_eng
  • 1,370
  • 3
  • 17
  • 40
0
votes
2 answers

Java threads - waiting on all child threads in order to proceed

So a little background; I am working on a project in which a servlet is going to release crawlers upon a lot of text files within a file system. I was thinking of dividing the load under multiple threads, for example: a crawler enters a directory,…
Mark Stroeven
  • 676
  • 2
  • 6
  • 24
0
votes
2 answers

How to wait for multiple Callables without blocking on get()

I'm performing a computation using java Callable and ExecutorService: ExecutorService threadExecutor = Executors.newCachedThreadPool(); for (TaskCallable task: tasks) { threadExecutor.submit(task); } I want to let the tasks run for at most 2…
MoneerOmar
  • 205
  • 2
  • 4
  • 18
0
votes
1 answer

Handler - why is my runnable not executing?

I have a runnable that I want to execute 4 times a second. Here is my runnable: shoot = new Runnable() { @Override public void run() { //Add bullet parts.add(new Part(1, (int)(screenWidth*.01), …
sadelbrid
  • 411
  • 1
  • 4
  • 16
0
votes
1 answer

Return value from server class

I need a return value from a server class that looks basicly like this. I need to return that object while the server keeps running. What is the easiest way to accomplish that? Object o = null; try { ServerSocket socketConnection = new…
claire
  • 1
0
votes
1 answer

cannot be cast to ThreadPoolTaskExecutor

I have to write a class which is accept the socket connection and pass it to Handler class using Spring My Listener Class is public class Listener { static Logger log = Logger.getLogger(Listener.class.getName()); public static void…
0
votes
1 answer

Update two TextViews inside a runnable

Can I update two textviews inside a runnable? Because my code can only update one textview. I got this method that updates 2 textviews that contains the address and date from the EXIF data of a photo. public void launchRingDialog() { final…
Jaye
  • 132
  • 1
  • 10
0
votes
0 answers

How can display ListView using an ArrayAdapter class and runOnUiThread method in my MainActivity?

When I try to run my app I get the following error: java.lang.ClassCastException: com.protogeo.moves.demos.apps.MainActivity$6 cannot be cast to android.content.Context at…
dbo
  • 161
  • 1
  • 10
0
votes
1 answer

Reading properties file outside jar

I have my jar file and properties file in same location (C:\test). When I run the jar through script, it throws FileNotFound exception for the properties file. This runs perfectly through RAD. What am I doing wrong here. I tried other ways, like…
Le_Master
  • 147
  • 1
  • 2
  • 20
0
votes
1 answer

Not able to resume my thread in android

I know there are tons of questions and answers on this topic here but I am not able to resolve the below issue to resume a thread in my app. heres is my runnable:- Runnable ViewPagerVisibleScroll= new Runnable() { @Override public void run()…
Himanshu Rathore
  • 108
  • 3
  • 11
1 2 3
99
100