Page 1 of 1
About Question enthuware.ocpjp.v7.2.1549 :
Posted: Wed Jun 04, 2014 6:42 am
by Crashtest
I wanted to answer "None of these" because I think it could be possible that a.interrupt(); is called before a.start() is scheduled by the OS to actually run (in rare occasion I guess).
Also, the answer "none of these" makes not much sense if you have to select two options... So this ruled it out for me straight away.
Re: About Question enthuware.ocpjp.v7.2.1549 :
Posted: Tue May 19, 2015 1:25 am
by pfilaretov
I agree with the previous post.
Also in this code
Code: Select all
public void run() {
System.out.println("Starting loop");
while(true){}; //1
System.out.println("Ending loop");
}
actually unreachable statement is semicolon (and not the println()), notice that it is NOT in the body of the while loop.
Re: About Question enthuware.ocpjp.v7.2.1549 :
Posted: Tue Aug 18, 2015 5:29 pm
by monseigneur
could you please clarify
Code: Select all
class A extends Thread {
public void run() {
System.out.println("Starting loop");
while(true){}; //1
System.out.println("Ending loop");
} }
public class TestClass {
public static void main(String args[]) throws Exception {
A a = new A();
a.start();
//1
Thread.sleep(1000); //2
a.interrupt(); //3
} }
what i reckon:
at //1 we have two threads running -
main and
a
by calling Thread.sleep(1000) at //2 we put to sleep one of them, the
current thread, and we don't know which one is it exactly (do we?)
so, if, say,
a is put to sleep and
main thread calls a.interrupt() meanwhile at //3, shouldn't
a throw an InterruptedException?
what am i missing here?
Re: About Question enthuware.ocpjp.v7.2.1549 :
Posted: Tue Aug 18, 2015 8:47 pm
by admin
monseigneur wrote:
what i reckon:
at //1 we have two threads running - main and a
by calling Thread.sleep(1000) at //2 we put to sleep one of them, the current thread, and we don't know which one is it exactly (do we?)
Of course we do. Sleep is being called from the main method so the thread executing the main method is the one that will go to sleep.
Re: About Question enthuware.ocpjp.v7.2.1549 :
Posted: Wed Aug 19, 2015 1:49 am
by monseigneur
great, thank you
Re: About Question enthuware.ocpjp.v7.2.1549 :
Posted: Sat Jan 30, 2016 9:44 pm
by krohani
Good curve ball question. I was concentrating so hard on Thread concepts trying to figure out what I am missing here that I didn't even bother to pay attention to the unreachable statement! Missed the question however!