Timeline for Simple yet threadsafe file cache in java
Current License: CC BY-SA 3.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 1, 2017 at 4:35 | history | edited | Tiny Rick | CC BY-SA 3.0 |
bug resolved
|
Nov 1, 2017 at 4:28 | comment | added | Tiny Rick | I see your point. Well the code posted in question does return a instance variable in the getLines() method. My bad ... :( In that case synchronization is necessary. But I would not say that is the best way. I will edit my answer with returning a local variable which is unique each reading threads , instead of returning instance variable. | |
Oct 31, 2017 at 20:12 | comment | added | Roland Illig | @TinyRick I disagree with your disagreement. See the documentation, especially the parts around the words surprising and paradoxical. | |
Oct 31, 2017 at 13:02 | comment | added | Tiny Rick | I disagree. The reading thread will get the uncorrupted data. That is ensured by retrying the read operation if the file is modified during the read operation. There is no need of synchronization between two threads which only reads. | |
Oct 31, 2017 at 12:54 | comment | added | Roland Illig |
The synchronized is still necessary. Otherwise one thread might see incomplete lines in the list. Read the whole Java Memory Model documentation for more information.
|
|
Oct 31, 2017 at 10:41 | comment | added | Tiny Rick | Thanks @Imus . Now beforeRead will be initialized just before the actual read operation. | |
Oct 31, 2017 at 10:39 | history | edited | Tiny Rick | CC BY-SA 3.0 |
more accurate solution
|
Oct 31, 2017 at 10:19 | comment | added | Imus |
The retry will never work like it is now though. Update the beforeRead right before retryCount--; That way you check if it has stayed the same in that retry attempt.
|
|
Oct 31, 2017 at 9:26 | vote | accept | amolkul | ||
Oct 31, 2017 at 9:25 | comment | added | Tiny Rick | Thats what we made sure in the while loop. We checked if the data has been changed at the time our read operation by once again checking the modified time with our pre-read modified time. So answering your question.. Yes without synchronization your list of file lines will be consistent with the file. | |
Oct 31, 2017 at 8:59 | comment | added | amolkul | Thanks Tiny Rick. But if I don't synchronize, will the list fileLines have consistent data ? | |
Oct 31, 2017 at 7:08 | history | answered | Tiny Rick | CC BY-SA 3.0 |