• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

TwoThreads problem with exception

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

I have a question about this code example (TwoThreads class). It comes from K&B's book, page 756.



Given the above code which letters will eventually appear somewhere in the output? (choose all that apply.)

A. A
B. B
C. C
D. D
E. E
F. F
G. The answer cannot be reliably determined
H. The code does not compile


the answer is:

A, C, D, E, F.

I don't understand why B is not a correct answer, as is it not possible that a InterruptedException (or any other random exception) occurs, therefore B might be printed? Or are these sort of things ignored in the exam. I thought the answer would be G, as it cannot be relibly determined if B will be output or not.

Plus, the book says that on the exam, the number of options for an answer will be given. In this case, does this mean that in the exam they will tell us 5 answers are correct? That would make this question infinitely more obvious.

Thanks for your time
Sok

[ July 30, 2006: Message edited by: Stephen O'Kane ]

[ July 30, 2006: Message edited by: Stephen O'Kane ]
[ July 30, 2006: Message edited by: Stephen O'Kane ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B is not correct as thread will return from sleep method after one second whereas, wait() method throws exception as current thread is not the owner of the lock.

Naseem
 
Stephen O'Kane
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Naseem,

I know that it will return from the sleep state after one second, but is it not possible that an exception could be thrown during that one second, causing 'B' to be printed?

Sok
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when its hardy time to run and it calls wait method on the terminated thread laurel, it throws exception and prints B.

Rishi
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when its hardy time to run and it calls wait method on the terminated thread laurel, it throws exception and prints B.


No B is never ever outputted like the book says youre not calling sleep on a the other thread, its a static method therefore whatever thread its called in will sleep. now AFAIK, the sleep() does throw an interruptedexception but for that to happen, some other thread must call the interrupt(). here even if you change the catch above S.O.P(B) to catch an InterruptedException and try to display B it wont because the laurel thread is not being interrupted. Again more input from ranchers would be appreciated.
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I didn't understand is that if the wait() method is not called from a synchronized context, the compiler should give error, right?
So it should not even compile.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait() method throws IllegalMonitorStateException if the current thread is not the owner of the object's monitor i.e., if wait() method is not called within a synchronized method or in a synchronized block.

check wait() method in Object class

Naseem
 
Stephen O'Kane
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, my brain understands it now, thanks Amitabha. After having a look at the API for InterruptedException;

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.



It seems that the only way for the piece of code where sleep() is called to throw an exception is if interrupt() is called on the thread that was executing when sleep() was called. Since no interrupt is called in the code, then we can assume no exception is thrown (I hope).

Thanks everyone
Sok
reply
    Bookmark Topic Watch Topic
  • New Topic