Mmh... I am waiting for Mark telling us that try ... finally constructs are too hard to be understood by a junior programer. Keep it simple guys !
"I'm not back." - Bill Harding, Twister
Although in truth, if the write throws an exception I don't think any of these designs have recovery mechanisms in place.
it turned out to be impractical to move the record locks into the Record class
That was true for the original ThreadSafeDatabase2 as well. If you have multiple Data instances, each one has a separate mutex object.
find(): Why TreeSet?
"I'm not back." - Bill Harding, Twister
Mmmm, actually I was talking about ThreadsafeDatabase2 and 4, not the cached ThreadsafeDatabase1.
..., the same arguments apply(For threadSafeData1). For the code shown, the recNos are unique and in order.
Incidentally it's possible to represent deleted records in a cache using nulls at appropriate List positions rather than an isDeleted flag
The concept of record-level synchronization and the concept of full caching need not go together.
In the examples illustrating the sleep(), join(), and wait() methods, you may have noticed that calls to each of these methods are wrapped in a try statement that catches an InterruptedException. This is necessary because the interrupt() method allows one thread to interrupt the execution of another. Interrupting a thread is not intended to stop it from executing, but to wake it up from a blocking state.
If the interrupt() method is called on a thread that is not blocked, the thread continues running, but its "interrupt status" is set to indicate that an interrupt has been requested. A thread can test its own interrupt status by calling the static Thread.interrupted() method, which returns true if the thread has been interrupted and, as a side effect, clears the interrupt status. One thread can test the interrupt status of another thread with the instance method isInterrupted(), which queries the status but does not clear it.
If a thread calls sleep(), join(), or wait() while its interrupt status is set, it does not block but immediately throws an InterruptedException (the interrupt status is cleared as a side effect of throwing the exception). Similarly, if the interrupt() method is called on a thread that is already blocked in a call to sleep(), join(), or wait(), that thread stops blocking by throwing an InterruptedException.
If a thread calls sleep(), join(), or wait() while its interrupt status is set, it does not block but immediately throws an InterruptedException (the interrupt status is cleared as a side effect of throwing the exception).
So it seems to be good practice Thread.currrentThread().interrupt() from within the catch block to keep the caller thread state unaffected by our own code.
But as nobody else than both of us gave his opinion about it yet (Andrew ? Max ?), I am still not sure about it.
So it seems to be good practice Thread.currrentThread().interrupt() from within the catch block to keep the caller thread state unaffected by our own code.
(From a much earlier post) If I remember, you just waited 48 hours to get the result, an amazing short time ! So yes, it's simply impossible to read everything so fast.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Yes, but it make then headackes later: you have to check if the thread is interrupted and so on.
I asked Max to at the topic, but he didn't find. I have sent him a link.
So, I hope that he we appear tonight in this topic.
In either case I cannot see why you would interrupt yourself.
In the first case, it would cause big problems. Thread goes into wait state. Something else interrupts it, throwing the Exception. As part of the Exception handling you interrupt the thread again, then go back to wait state which is immediately interrupted because of our interrupt which goes into the Exception handler which interrupts .....
the interrupt status is cleared as a side effect of throwing the exception
I can see one more disadvantage of RWSync. There are some nested locks in the code.
They are not dangerous, I have tested it, but still it is not nice.
In your second code snippet, your first comment is false
It's more a question of beeing reasonable in nesting, beeing aware of them, and be very very cautious
Max has strongly critized having a multiple or/and nested locks
(1) = concurrent read access on Data
(2) = concurrent read access on Cache
I don't see another way of doing.
There are two other designs ThreadSafeData1 (Jim) and ThreadSafeData4 (Andrew)
I just want to hear from as many people as possible that it is cool Otherwise I seriously consider switching to one of other 2 designs
organize some opinion poll on the subject and follow the majority
This implementation supports the interruption of lock acquisition and provides a Condition implementation that supports the interruption of thread suspension. It also favors interruption over normal method return.
I will leave you after tomorrow for a week, because of having vacation. I deserve some rest from hard arguing with Phil (Just a joke).
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I think that some of the solutions presented here are far too complex for such a simple assignment.
(...)
if Philippe allows us all to have a break (just kidding Phil)).
you knew there was going to be a but didn't you
Have a great holiday - we can always take this up again when you get back (if Philippe allows us all to have a break (just kidding Phil)).
Just as an FYI, I would argue that the InteruptedException should cause an explicit fatal exception to thrown: one that ripples up to middle tier as a FatalSystemException, and eventually to the user as a FatalGUIException, and which cause the system to shutdown. This is how most apps deal with this sort of thing in the real world.
Andrew, you have said that find() in ThreadSafeData4 is thread safe: if another thread updates/deletes a record, the find method still hold a reference to the old copy.
In this case Andrew is right:
record = new Record(.., ..., ..);
list.set(recNo, record);
Another solution is that read return always a clone of the record, but it can cause memory overhead.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I don't think we need to be too concerned about the memory issues of the clone
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|