The Output is: The above code prints A My question is: When we pass the Runnable target as B then why does it invoke the run() method of A. Although we invoke A's constructor only. Thanks
When you call the .start method of a Thread class, according to the JavaDoc: [calling the start method] causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread So, the run method will be called. Since A's start method is used and A's run method prints A, A is printed. The runnable passed to A's constructor has no effect.
maybe you could look at the Thread.java. You will see that the run method there looks like this :
And when start of the thread is invoked it will do the run() method. And you overridden the run() method in the original thread. So it won't execute the 'target' but only your run method. Look in the source of java wich comes with every SDK. I hope this helps greeting Jos ( who is has to take his exam april the 6th ) [ April 02, 2004: Message edited by: j zaal ]