-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
File decompression uses new class StringKeeper to make sure a file is… #22
Conversation
… being handled only by one thread at a time
* @author cmrose | ||
*/ | ||
|
||
public class StringKeeper { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really a type of Lock (see https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html and https://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/) - perhaps StringLock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is now called StringLocker
private List stringList = Collections.synchronizedList(new ArrayList<String>()); | ||
private boolean waiting = false; | ||
|
||
public synchronized void control(String item) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will block all threads trying to control an item not just ones trying to access the item won't it? (only one thread can be running this particular bit of code at a time) Will have the desired effect still but is overly restrictive. Could have just added synchronized to makeUncompressed above to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that the wait() will cause the thread to wait and to no longer be synchronised on the the object thus allowing another thread to come in and add a file to the list. https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait This is evidenced to be the case by the fact that I observe more than one file appearing in the list at any given time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, thanks for the explanation. Can see how that works now.
} | ||
} | ||
|
||
stringKeeper.release(filename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better in a single try finally block so we always know its going to be released?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done so in the second commit. There was already a try..catch in the method that calls makeUncompressed (getRaf) which I added the finally to.
Move calls to stringLocker.control() and stringLocker.release() into try..catch..finally in NetcdfFile.getRaf for more easily read code and guaranteed release of the lock.
#22) File decompression uses new class StringLocker to make sure a file is being handled only by one thread at a time
… being handled only by one thread at a time