aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Problem" Watch "Thread Problem" New topic
Author

Thread Problem

garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Source: http://www.examlab.tk/ Practice Exam: 3 Question No.: 28


Output is "EX-B followed by Exception". Why EX-A not printed before EX-B. a.interrupt should interrupt the new thread and cause it to generate exception. Please clarify. Thanks.
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Hi,

Here interrupt() method is called on object a which is not a thread of execution.(its just an instance of subclass of Thread) And a separate thread is started using t.start(). Thats the reason for the answer!

And see the API documentation of interrupt() method!

It will throw InterruptedException only if the thread is currently blocked(either sleeping,waiting or blocked for object locks). Otherwise it will just set the interrupted flag and run as it is.

Hope this helps!


Thanks,<br />Srilatha M
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Do we have the interrupt() call for SCJP 6?
Kenneth Lomvey
Ranch Hand

Joined: Nov 08, 2008
Posts: 94
Originally posted by Rekha Srinath:
Do we have the interrupt() call for SCJP 6?


Yes, certainly.
interupt(), interrupted() and isInterrupted() will be on the exam. :roll:
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
this question is tai sun zi la !!!


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Just now went through the code in detail:

The Ex-B catch block actually throws IllegalMonitorStateException, why?

There is also another IllegalMonitorStateException thrown from main, which I understand, is because there is no synchronized block for notifyAll()
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Hi Rekha,

When a thread calls a wait() or notify() method on an object, it has to own that object's lock. And those method calls should be in synchronized context!

Here when wait() method is called inside halt(), its not called from synchronized context so the exception will be thrown.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Oooo... Srilatha...I missed that..thanks for pointing it out...
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Thanks Srilatha..i got it
garima jain
Ranch Hand

Joined: Sep 22, 2008
Posts: 60
Had it been t.interrupt, exception EX-A would have been generated. Am I right?
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
These questions are designed to be tricky, right? No one actually writes code that is this confusing for production, right?

"Comment your code please" <-- (what I'd be saying to the coder of this)


SCJA
~Currently preparing for SCJP6
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Had it been t.interrupt, exception EX-A would have been generated. Am I right?


Yes Garima...I agree with you
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Problem