This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from examlab on threads 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 "Question from examlab on threads" Watch "Question from examlab on threads" New topic
Author

Question from examlab on threads

ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
hi ,



This is a question from examlab. It outputs Ex-B and then an exception.
Although a nice explanation is given in examlab. But i could not understand the interrupt
call on line 2.
According to me the working should be :

Line 1 should start the thread t and then doDelay should make t sleep for 1 sec.
Meanwhile main calls dodelay() which should put main to sleep.
But main should wake you before t and then call interrupt. This must make t throw
IntrruptedException and print Ex-A. Now thread t should resume and call halt() and
calling wait() should throw Exception and print Ex-B..

Please correct me if i am wrong.


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

There is a slight trick here, we actually started thread t and called interrupt on a. This is why the call to a.interrupt() didn't throw an exception. If you call t.interrupt() instead of a.interrupt(), you'll get "Ex-A" in the output...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Yes Ankit it works..
Thanks but i want to know that the execution which i stated is correct or not. I am having much problem in getting threads..
Please let me know
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Yes your interpretation of the flow of execution was right...
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
Ok dear thanks very much
 
GeeCON Prague 2014
 
subject: Question from examlab on threads