简体   繁体   中英

Why writing new Runnable in the new Thread?

Can anyone please explain why one writes new Runnable in the new Thread? Fist time I saw this at android developers, please see the code in the link below. Also, could you tell why we need to implement notifications in separate thread?

http://developer.android.com/training/notify-user/display-progress.html

I used to create threads by new Thread or with Runnable but not together, so got a bit confused..

This is an just a lazy example that whoever set up that page wrote. Likely because it needed to be condensed and short.

All it's showing is how you display progress in the notification bar. What it's trying to tell you is that you can show progress in the notification bar during lengthy background operations. It's thread safe and it doesn't have to be main UI thread, so feel free to display progress wherever you need it. Usually you don't use Thread.sleep during an operation while incrementing a counter. In reality, to show the progress, you have to decide what the progress value represents during the course of the operation.

Thread s are java objects that represent threads of execution. Runnable is an interface for objects that represent some code intended to be executed by Thread s. Two concepts, two types (execution-> Thread , code-> Runnable ).

For simplicity (?) Thread s are also Runnable s, so that in simple cases you just have to subclass Thread and override its run method (default implementation of run method does nothing). If you don't give a Runnable to a Thread , then it uses itself as the Runnable to execute.

we can create a thread in two ways

by extending Thread class and by implementing Runnable interface

if we create a class by extending Thread class like below

class ThreadDemo extends Thread{
public void run(){
//job of a thread
}
public static void main (String[] args){
ThreadDemo d = new ThreadDemo();
d.start();
}
}

you can directly call start() and jvm internally calls run() method so that you can define job of Thread in run() by overriding it in your class. But if you create Thread by implementing Runnable(I) like below.

class ThreadDemo implements Runnable{
public void run(){
//job of Thread 
}
public static void main(String[] args){
ThreadDemo d = new ThreadDemo(); //d.start() is not possible
Thread t = new Thread(d);//where d is target runnable
t.start()
}
}

you cant call start() directly with ThreadDemo instance because Runnable(I) contains only run() method, so you have to pass your ThreadDemo instance to Thread instance at the time of Thread creation it initializes your target Runnable instance and calls Runnable run() method not Thread class run() method.

new Thread(
    new Runnable() {
       ......
       .....
 }
    }
).start(); 

which is similar to

ThreadDemo d = new ThreadDemo(); //d.start() is not possible
    Thread t = new Thread(d);//where d is target runnable
    t.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM