Re: Immutable SyntexCheckers
Le 20/03/2017 à 08:20, Stefan Seelmann a écrit : > On 03/20/2017 08:12 AM, Emmanuel Lécharny wrote: >> >> Le 20/03/2017 à 07:37, Stefan Seelmann a écrit : >>> On 03/20/2017 02:10 AM, Emmanuel Lécharny wrote: public class BooleanSyntaxChecker extends SyntaxChecker { /** * A static instance of BooleanSyntaxChecker */ public static final BooleanSyntaxChecker INSTANCE = new BooleanSyntaxChecker( SchemaConstants.BOOLEAN_SYNTAX ); /** A static instance of the builder */ private static final Builder BUILDER_INSTANCE = new Builder(); /** * @return An instance of the Builder for this class */ public static Builder builder() { return BUILDER_INSTANCE; } >>> Hm, why a static builder? As it is not immutable there's a chance of >>> race condition in case two threads use it concurrently. >> That can't happen, because we have : >> >> private static final Builder BUILDER_INSTANCE = new Builder(); >> >> that is guaranteed to be built during the class loading. > I agree about the creation of the builder instance. > > But if two threads *use* it: > 1. thread 1 calls builder() > 2. thread 2 calls builder() and gets the same builder instance > 3. thread 1 calls setOid("1.1.1") > 4. thread 2 calls setOid("2.2.2") > 5. thread 1 calls build() and may get an SC with OID "2.2.2"! Good catch... /** * @return An instance of the Builder for this class */ public static Builder builder() { return new Builder(); } would solve the issue, correct ? (somtime, trying to verdue is wrong). > > -- Emmanuel Lecharny Symas.com directory.apache.org
Re: Immutable SyntexCheckers
On 03/20/2017 08:12 AM, Emmanuel Lécharny wrote: > > > Le 20/03/2017 à 07:37, Stefan Seelmann a écrit : >> On 03/20/2017 02:10 AM, Emmanuel Lécharny wrote: >>> public class BooleanSyntaxChecker extends SyntaxChecker >>> { >>> /** >>> * A static instance of BooleanSyntaxChecker >>> */ >>> public static final BooleanSyntaxChecker INSTANCE = new >>> BooleanSyntaxChecker( SchemaConstants.BOOLEAN_SYNTAX ); >>> >>> /** A static instance of the builder */ >>> private static final Builder BUILDER_INSTANCE = new Builder(); >>> /** >>> * @return An instance of the Builder for this class >>> */ >>> public static Builder builder() >>> { >>> return BUILDER_INSTANCE; >>> } >> Hm, why a static builder? As it is not immutable there's a chance of >> race condition in case two threads use it concurrently. > > That can't happen, because we have : > > private static final Builder BUILDER_INSTANCE = new Builder(); > > that is guaranteed to be built during the class loading. I agree about the creation of the builder instance. But if two threads *use* it: 1. thread 1 calls builder() 2. thread 2 calls builder() and gets the same builder instance 3. thread 1 calls setOid("1.1.1") 4. thread 2 calls setOid("2.2.2") 5. thread 1 calls build() and may get an SC with OID "2.2.2"!
Re: Immutable SyntexCheckers
Le 20/03/2017 à 07:37, Stefan Seelmann a écrit : > On 03/20/2017 02:10 AM, Emmanuel Lécharny wrote: >> public class BooleanSyntaxChecker extends SyntaxChecker >> { >> /** >> * A static instance of BooleanSyntaxChecker >> */ >> public static final BooleanSyntaxChecker INSTANCE = new >> BooleanSyntaxChecker( SchemaConstants.BOOLEAN_SYNTAX ); >> >> /** A static instance of the builder */ >> private static final Builder BUILDER_INSTANCE = new Builder(); >> /** >> * @return An instance of the Builder for this class >> */ >> public static Builder builder() >> { >> return BUILDER_INSTANCE; >> } > Hm, why a static builder? As it is not immutable there's a chance of > race condition in case two threads use it concurrently. That can't happen, because we have : private static final Builder BUILDER_INSTANCE = new Builder(); that is guaranteed to be built during the class loading. -- Emmanuel Lecharny Symas.com directory.apache.org