I have a simple thread test code snippet,its pseudo code like this new Thread(new Runnable(){...implement run method....}){...override run method...}.start();
. my problem is why does this code output is 'thread:Thread..' ,not 'runnable:Thread..'?here is the complete code.
public class ThreadTest {
public static void main(String[] args) {
new Thread(new Runnable(){//1 annonymous class implement Runnable interface
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("runnable:" + Thread.currentThread().getName());
}
}){
public void run() {//2 annonymous class extends Thread class
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread:" + Thread.currentThread().getName());
};
}.start();
}
}
By default, Thread
's run
method will delegate to the Runnable
you supplied in the constructor.
However, you overrode the run
method in your (anonymous) subclass of Thread
to do something else.