| Author |
Interrupt Confusion
|
Arhaan Singhania
Ranch Hand
Joined: Mar 28, 2011
Posts: 89
|
|
Hello,
This is a sample question, i made wrong on the Enthuware test. It is given below
The answer is 1 and 2.
I have understood the working of code but still i have two queries
1-Can someone explain me this code sample in detail. I am having confusion in interrupt and all.
2- What does the explanation of option 4 mean which i have commented.
P.S We needed to pick two options.
Thanks,
Arhaan
|
 |
yusuf Kaplan
Ranch Hand
Joined: Mar 11, 2011
Posts: 56
|
|
Arhaan Singhania wrote:
1- This will not compile.
2- It will run and end cleanly...
The answer is 1 and 2.
Hey, are you sure about the correct answers?
How can it on the one hand not compile and on the other hand run??
|
 |
Arhaan Singhania
Ranch Hand
Joined: Mar 28, 2011
Posts: 89
|
|
Hello Yusuf,
It asked me two options to click.
As you can see in this code block
In line 1, as this block of code will run forever, so there is no way, it can reach ending loop statement which gives an error
of Unreachable code. Second option is how we can replace this while block in order to make this code correct.
There were two options which we needed to select for the question.
Thanks,
Arhaan
|
 |
yusuf Kaplan
Ranch Hand
Joined: Mar 11, 2011
Posts: 56
|
|
I don't understand it.
How can you get the code running without compiling with no errors?
|
 |
Arhaan Singhania
Ranch Hand
Joined: Mar 28, 2011
Posts: 89
|
|
Hello,
Yusuf, this question asked for multiple choices to select. 2 choices.
One first glance, if you look the code, you can very well click that, this code won't compile because of the while
statement. Option 1 and now if you replace this while block with option2, code will compile too and run fine as well.
Did you get me?
Arhaan
|
 |
Andreas Svenkson
Ranch Hand
Joined: Jan 17, 2011
Posts: 178
|
|
Interesting tricky question, I enjoyed reading it.
I'm not sure if you were wondering anything more now? Regarding option 4, you have written yourself why in fact it will not end with an InterruptedException, (because the second thread is not blocked with wait(), sleep() or join()), but rather it is continually running, checking a condition until it is not met.
It's worth noting btw, that if instead of constructing the while-loop with a literal 'true', if you use a boolean variable "b = true", it will compile (and run forever), likewise if the while-loop does check for the literal 'true' but there are no statements following it.
// Andreas
|
 |
Arhaan Singhania
Ranch Hand
Joined: Mar 28, 2011
Posts: 89
|
|
Hello,
What i actually wanted to know was a complete role of interrupt in it. Specially, which thread will go to sleep
will it be main or Thread t. Nothing can be said for sure. So i was looking for a little background of this whole
code example.
Arhaan
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Well the interrupt method call as the explanation says only throws an exception in the target thread if the thread is sleeping or waiting. If the target thread is executing normally, only a flag is set for that thread which can be checked using the call to isInterrupted method...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
 |
|
|
subject: Interrupt Confusion
|
|
|