You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@jena.apache.org by GitBox <gi...@apache.org> on 2020/03/14 18:01:11 UTC

[GitHub] [jena] intrigus-lgtm commented on issue #709: Add volatile to make method thread-safe

intrigus-lgtm commented on issue #709: Add volatile to make method thread-safe
URL: https://github.com/apache/jena/pull/709#issuecomment-599111184
 
 
   The problem with the current code is this:
   Even if we write to `acceptedParams_` [here](https://github.com/apache/jena/pull/709/files#diff-f3592fdcfd01af694d9a604a977a972fL102),
   https://github.com/apache/jena/pull/709/files#diff-f3592fdcfd01af694d9a604a977a972fL105 can **still** return `null`.
   The Java Memory Model (JMM) evaluates every read in isolation, such that
   if there are two reads, the first read could see a non-null value and the second read could see a null value.
   So consider this:
   (Time progresses from 1 to 4)
   Thread 1
   1. calls `acceptedParams(action)`
   2. writes to `acceptedParams_`
   Thread 2
   3. Calls `acceptedParams(action)`
   4. Sees that `acceptedParams_` is not null and returns it
   
   The thread 2 then may **still not** see a completely initialized Set.
   Just because thread 2 sees `acceptedParams` being not null, it does not have to see any of the stores, that add the values to the Set.
   
   This can only be fixed by adding volatiles or using one of the methods detailed [here](https://shipilev.net/blog/2014/safe-public-construction/#_safe_publication).
   
   **Caveat: I'm no JMM expert, so take everything I say with some caution**

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@jena.apache.org
For additional commands, e-mail: pr-help@jena.apache.org