About Question enthuware.ocpjp.v7.2.1549 :
Moderator: admin
-
- Posts: 18
- Joined: Fri May 31, 2013 1:18 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1549 :
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.
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.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1549 :
I agree with the previous post.
Also in this codeactually unreachable statement is semicolon (and not the println()), notice that it is NOT in the body of the while loop.
Also in this code
Code: Select all
public void run() {
System.out.println("Starting loop");
while(true){}; //1
System.out.println("Ending loop");
}
-
- Posts: 6
- Joined: Sun Aug 09, 2015 6:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1549 :
could you please clarify
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?
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
} }
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1549 :
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.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?)
-
- Posts: 6
- Joined: Sun Aug 09, 2015 6:55 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1549 :
great, thank you
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1549 :
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!
Who is online
Users browsing this forum: No registered users and 6 guests