| Summary: | [api] provide thread safety for cachedRemoteFiles hashmap | ||
|---|---|---|---|
| Product: | [Tools] Target Management | Reporter: | David McKnight <dmcknigh> |
| Component: | RSE | Assignee: | David McKnight <dmcknigh> |
| Status: | RESOLVED FIXED | QA Contact: | Martin Oberhuber <mober.at+eclipse> |
| Severity: | normal | ||
| Priority: | P3 | CC: | xuanchen |
| Version: | 3.2 | ||
| Target Milestone: | 3.4 M5 | ||
| Hardware: | PC | ||
| OS: | Windows XP | ||
| Whiteboard: | |||
| Bug Depends on: | |||
| Bug Blocks: | 368456 | ||
| Attachments: | |||
|
Description
David McKnight
Created attachment 209382 [details]
patch to provide thread safety for cachedRemoteFiles
I've committed the fix to the HEAD stream. The fix looks good. Thanks. The _cachedRemoteFiles field is protected, and therefore it is API. When you make all access to that field synchronized, you should add a Javadoc comment to the field indicating that any access to it must be synchronized -- or extenders of the class could ruin your thread-safety. Besides adding the Javadoc which I'd expect, there's a 2nd thing we can learn here... usually, instead off adding your synchronized blocks everywhere, I would have done this: protected HashMap _cachedRemoteFiles = Collections.synchronizedMap(new HashMap()); but, unfortunately, your API exports type "HashMap" and not type "Map" so this is not possible without breaking API. What we learn from this is that API classes should never be concrete classes (like HashMap) but generic classes or interfaces (like Map). Created attachment 210380 [details]
amendment to patch with javadoc comment about field synchronization
(In reply to comment #4) > The _cachedRemoteFiles field is protected, and therefore it is API. When you > make all access to that field synchronized, you should add a Javadoc comment to > the field indicating that any access to it must be synchronized -- or extenders > of the class could ruin your thread-safety. I've added a javadoc comment for this. > > Besides adding the Javadoc which I'd expect, there's a 2nd thing we can learn > here... usually, instead off adding your synchronized blocks everywhere, I > would have done this: > > protected HashMap _cachedRemoteFiles = Collections.synchronizedMap(new > HashMap()); > > but, unfortunately, your API exports type "HashMap" and not type "Map" so this > is not possible without breaking API. What we learn from this is that API > classes should never be concrete classes (like HashMap) but generic classes or > interfaces (like Map). Good point and certainly a good lesson to go forward with. I've committed the update to the HEAD stream. Thanks for the Javadoc blurb but I think it's not ideal yet:
1. You may want to discourage using the Field altoghether
(with an @noreference tag) as well as set an @deprecated tag in order
to enable us make the field non-API in the future
2. The first sentence in any Javadoc comment should be a "summary" kind of
description of the item
3. If you require synchronizing, you need to specify what object users should
synchronize on (the hasmap itself, in this case, which is not exactly
obvious). A small example code snippet would make sense here.
In fact, I think the code would benefit if you add an
RemoteFileSubSystem#clearRemoteFileCache()
protected method, and use that new method in FileServiceSubSystem (such that
you don't have to have the field protected any more).
Created attachment 210389 [details]
amendment to patch with javadoc comment about field synchronization
How's this updated comment? I can't add the @noreference right now because the compiler will complain.
Created attachment 210392 [details]
amendment to patch with new clearRemoteFileCache() api
Martin, with the patch amendment that deprecates _cachedRemoteFiles, do you thing it's still necessary to show the code example? I think we can mark this fixed, right ? (In reply to comment #12) > I think we can mark this fixed, right ? Yes, this is fixed. |