About Question com.enthuware.ets.scjp.v6.2.276 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
MC20147
Posts: 9
Joined: Sat Feb 25, 2012 3:51 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.276 :

Post by MC20147 »

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

admin
Site Admin
Posts: 10095
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.276 :

Post by admin »

No, even if the other thread enters infinite loop, the main thread is still in Runnable state and so the JVM will schedule it some time or the other, so it will reach completion and will print "main returns" as some point.
In fact, on a multi-core CPU it is possible that the JVM may not pause the main thread at all.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

MC20147
Posts: 9
Joined: Sat Feb 25, 2012 3:51 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.276 :

Post by MC20147 »

Excellent,

That all makes sense.

Thanks for the clarification.

Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests