new Runnable() but no new thread?

113,385

Solution 1

Runnable is often used to provide the code that a thread should run, but Runnable itself has nothing to do with threads. It's just an object with a run() method.

In Android, the Handler class can be used to ask the framework to run some code later on the same thread, rather than on a different one. Runnable is used to provide the code that should run later.

Solution 2

If you want to create a new Thread...you can do something like this...

Thread t = new Thread(new Runnable() { public void run() { 
  // your code goes here... 
}});

Solution 3

The Runnable interface is another way in which you can implement multi-threading other than extending the Thread class due to the fact that Java allows you to extend only one class.

You can however, use the new Thread(Runnable runnable) constructor, something like this:

private Thread thread = new Thread(new Runnable() {
public void run() {
   final long start = mStartTime;
   long millis = SystemClock.uptimeMillis() - start;
   int seconds = (int) (millis / 1000);
   int minutes = seconds / 60;
   seconds     = seconds % 60;

   if (seconds < 10) {
       mTimeLabel.setText("" + minutes + ":0" + seconds);
   } else {
       mTimeLabel.setText("" + minutes + ":" + seconds);            
   }

   mHandler.postAtTime(this,
           start + (((minutes * 60) + seconds + 1) * 1000));
   }
});

thread.start();

Solution 4

You can create a thread just like this:

 Thread thread = new Thread(new Runnable() {
                    public void run() {

                       }
                    });
                thread.start();

Also, you can use Runnable, Asyntask, Timer, TimerTaks and AlarmManager to excecute Threads.

Solution 5

Runnable is just an interface, which provides the method run. Threads are implementations and use Runnable to call the method run().

Share:
113,385
sgarg
Author by

sgarg

Updated on July 05, 2022

Comments

  • sgarg
    sgarg almost 2 years

    I'm trying to understand the code here , specifically the anonymous class

    private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {
       final long start = mStartTime;
       long millis = SystemClock.uptimeMillis() - start;
       int seconds = (int) (millis / 1000);
       int minutes = seconds / 60;
       seconds     = seconds % 60;
    
       if (seconds < 10) {
           mTimeLabel.setText("" + minutes + ":0" + seconds);
       } else {
           mTimeLabel.setText("" + minutes + ":" + seconds);            
       }
    
       mHandler.postAtTime(this,
               start + (((minutes * 60) + seconds + 1) * 1000));
       }
    };
    

    The article says

    The Handler runs the update code as a part of your main thread, avoiding the overhead of a second thread..

    Shouldn't creating a new Runnable class make a new second thread? What is the purpose of the Runnable class here apart from being able to pass a Runnable class to postAtTime?

    Thanks

  • sgarg
    sgarg over 12 years
    so does this example create a new thread or not, because you have used Thread but not called start()..
  • npinti
    npinti over 12 years
    @shishirgarg: This example creates a new Thread object. To execute it, simply call the start() method like so: thread.start(). That should internally call the run() method (note, you should never call the run() method yourself) which kick starts the thread.
  • glaz666
    glaz666 over 11 years
    "Runnable itself has nothing to do with threads" this is conceptually WRONG! Please read documentation. Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. It is also confusing when Runnable is passed, but stuff happens in the same thread. Please read about "principle of least surprise".
  • Wyzard
    Wyzard over 11 years
    @glaz666, the Runnable documentation talks about threads because they're the most common use of Runnable, but the interface is suitable for any kind of callback. I think the Android Handler's use of Runnable is entirely reasonable — it's better than defining a new Android-specific interface that's identical to Runnable but with a different name.
  • alfoks
    alfoks about 9 years
    Not what the OP asked.
  • Adam Howell
    Adam Howell over 8 years
    An example of "new Runnable()" without "extends Thread" nor "implements Runnable": docs.oracle.com/javase/tutorial/uiswing/examples/start/…
  • RoundSparrow hilltx
    RoundSparrow hilltx over 7 years
    you can override the run() method of Thread. What's the benefit of creating the Runnable too?
  • splungebob
    splungebob almost 6 years
    @glaz666, I've never liked the wording in Runnable's javadoc (that you quoted earlier), and apparently the Java folks agreed that it was too narrow. Notice how they improved the same wording in Callable's javadoc: "The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread."