About Question com.enthuware.ets.scjp.v6.2.276 :
Posted: Sat Feb 25, 2012 4:18 pm
The question gives the following code:
public class TestClass extends Thread
{
public void run()
{
for(;;);
}
public static void main(String args[])
{
System.out.println("Starting Main");
new TestClass().start();
System.out.println("Main returns");
}
}
My understanding is that after the call to start() the Thread is placed into runnable status and then one of two things can happen:
1 - The main thread will continue and execute the next line ("Main Returns") and then the new Thread will execute the endless loop.
2 - The thread scheduler could place the invoking Thread (main in this case) back to a runnable state and then the new Thread could execute the endless loop. In this case the "main" thread would never reach the last line of code ("Main returns").
Therefore, the only guarantee in the above code is that "Starting Main" will print?
Is the above correct or will it ALWAYS be the case that the Main method will execute the following line after the call to start() ?
Thanks,
Mark
public class TestClass extends Thread
{
public void run()
{
for(;;);
}
public static void main(String args[])
{
System.out.println("Starting Main");
new TestClass().start();
System.out.println("Main returns");
}
}
My understanding is that after the call to start() the Thread is placed into runnable status and then one of two things can happen:
1 - The main thread will continue and execute the next line ("Main Returns") and then the new Thread will execute the endless loop.
2 - The thread scheduler could place the invoking Thread (main in this case) back to a runnable state and then the new Thread could execute the endless loop. In this case the "main" thread would never reach the last line of code ("Main returns").
Therefore, the only guarantee in the above code is that "Starting Main" will print?
Is the above correct or will it ALWAYS be the case that the Main method will execute the following line after the call to start() ?
Thanks,
Mark