The Java docs state that if we supplied a Runnable target when creating a new thread, .start() of that thread would run the run() method of the supplied runnable.
If that’s the case, shouldn’t this test code prints “a” (instead of printing “b”) ?
public class test {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("a");
}
};
Thread t = new Thread(r) {
@Override
public void run() {
System.out.println("b");
}
};
t.start();
}
}
Because you are overriding Thread.run() method.
Here is the implementation of Thread.run():
try:
You will get
ab.