Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Problem

 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we have the interrupt() call for SCJP 6?
 
Kenneth Lomvey
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this question is tai sun zi la !!!
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oooo... Srilatha...I missed that..thanks for pointing it out...
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Srilatha..i got it
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had it been t.interrupt, exception EX-A would have been generated. Am I right?
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had it been t.interrupt, exception EX-A would have been generated. Am I right?


Yes Garima...I agree with you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic