• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InterruptedException for lock()

 
Rudolph Jen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys.

I guess that I am allowed to ask this, and so I go:

I am just thinking about the lock(int recNo) of the data-access-system (Data.java). In the assignment is written "Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available."

That cries for wait() and notify(). But the wait() Method of Thread throws the checked InterruptedException. Uncheck Exception has to be thrown but this would change the supplied interface Data.java.

Any ideas?

Best regards
Rudolph
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rudolph,

The InterruptedException handling is your decision.
You can rethorw it or you can ignore it and just log it, but if you choose to rethow it take care that this interrupts the normal work flow (and if you dont take care worst you can pass over the lock).

Regards M.
 
Rudolph Jen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mihai,

thanks for your replay.

I do NOT want to handle that Exception but I have to, if I use wait().

API:
InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

Okay, that should not happen in my implementation. But if it happens in any way, then my code would not work properly. Perhaps I don't see the forest because of all the trees, but I still don't get it.

1. use wait() => then you have to deal what happens in case of an InterruptedException.
2. don't use wait() => I don't see how it could be this way?

;-(
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rudolph Jen:
Mihai,

thanks for your replay.

I do NOT want to handle that Exception but I have to, if I use wait().

API:
InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

Okay, that should not happen in my implementation. But if it happens in any way, then my code would not work properly. Perhaps I don't see the forest because of all the trees, but I still don't get it.

1. use wait() => then you have to deal what happens in case of an InterruptedException.
2. don't use wait() => I don't see how it could be this way?

;-(


Have a read of this:

http://www-128.ibm.com/developerworks/java/library/j-jtp05236.html
 
Rudolph Jen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for that link.
Guess I got my answer.

regards
Rudolph
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic