What will happen when you attempt to compile and run the following code?public class MyThread extends Thread
{
public static void main(String argv[])
{
MyThread mt = new MyThread();
mt.run();
} public void start()
{
for (int i = 0; i <10; i++)
{
System.out.println("Value of i = " + i);
}
}
}解答:
D is correct. If you look carefully, in the above code the run() method is invoked on Thread class. To start a Thread, the start() method is called which in turn invokes the run() method. The run method of Thread class does nothing. Thus nothing will be printed out. Thus C is incorrect. A is incorrect as there is a run method in the Thread class. B is incorrect as there will not be any run time error on running the above code.
{
public static void main(String argv[])
{
MyThread mt = new MyThread();
mt.run();
} public void start()
{
for (int i = 0; i <10; i++)
{
System.out.println("Value of i = " + i);
}
}
}D正确,如果你仔细看,在上述代码中run()方法被Thread类执行。开始一个线程,当run()被执行时start()方法被调用。Thread类中的run()方法什么事都没做。所以没用东西会被打印出来。故C不正确。A错误因为Thread类中有run()方法。B错误因为运行时不会有任何错误出现。
mt.run();
在这句调用了run方法...这题是考验你对线程的掌握了...