new Runnable() but no new thread? new Runnable() but no new thread? multithreading multithreading

new Runnable() but no new thread?


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.


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... }});


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();