• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from examlab on threads

 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes your interpretation of the flow of execution was right...
 
ujjawal rohra
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok dear thanks very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic