Page 1 of 1

About Question enthuware.ocpjp.v8.2.1549 :

Posted: Wed May 11, 2016 3:02 pm
by schchen2000
In this question, we've got 2 threads. One thread launched by calling a.start() and the other thread being main thread.

When you say
Thread.sleep(1000);
which thread are you trying to put to sleep? I think it's the main thread that would go to sleep. Is that correct?

In this question, the InterruptedException WON'T be thrown because the thread launched by a.start() was never on wait(), join() or sleep() when a.interrupt() was called. Do I understand this correctly?

Many thanks.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1549 :

Posted: Wed May 11, 2016 6:52 pm
by admin
1. Correct. The thread that calls sleep is the one that goes to sleep.
2. Correct.

Re: About Question enthuware.ocpjp.v8.2.1549 :

Posted: Wed May 11, 2016 10:00 pm
by schchen2000
Thank you.

Schmichael

Re: About Question enthuware.ocpjp.v8.2.1549 :

Posted: Wed Jun 27, 2018 4:54 am
by Harvey Manfrenjensen
By the way, if System.out.println("Ending loop"); is commented out,
the code still can not be compiled because of empty statement behind the while loop.
Phhhuuu... :-)

Regards, Paul

Re: About Question enthuware.ocpjp.v8.2.1549 :

Posted: Wed Aug 08, 2018 11:28 am
by __JJ__
calling interrupt() will cause an InterruptedException ONLY if the thread on which interrupt() is called, is blocked on an invocation of the wait(...), join(...), or sleep(...), methods of this class.
Hi
The above surely cannot be true, for otherwise there would not be hundreds of methods that declare they throw InterruptedException. I apologize if I'm missing something, but you have capitalized the word only, and I cannot see that the statement is correct. There are thread states of BLOCKED and WAITING; any thread in WAITING I would've thought would throw an IE when interrupted (but I don't know about blocked).

Thanks.

Re: About Question enthuware.ocpjp.v8.2.1549 :

Posted: Wed Aug 08, 2018 10:14 pm
by admin
Calling other methods may also throw an IE but this is talking about the case when calling interrupt on a thread will throw an InterruptedException.