- java.lang.Object
-
- java.lang.Enum<SynchronizationStrategy>
-
- io.github.bucket4j.local.SynchronizationStrategy
-
- All Implemented Interfaces:
Serializable,Comparable<SynchronizationStrategy>
public enum SynchronizationStrategy extends Enum<SynchronizationStrategy>
Defines the strategy of synchronization which need to be applied to prevent data-races in multithreading usage scenario.- Since:
- 1.3
-
-
Enum Constant Summary
Enum Constants Enum Constant Description LOCK_FREELock-free algorithm based on CAS(compare and swap) of immutable objects.NONEThis is fake strategy which does not perform synchronization at all.SYNCHRONIZEDBlocking strategy based on javasynchronizedkeyword.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static SynchronizationStrategyvalueOf(String name)Returns the enum constant of this type with the specified name.static SynchronizationStrategy[]values()Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
LOCK_FREE
public static final SynchronizationStrategy LOCK_FREE
Lock-free algorithm based on CAS(compare and swap) of immutable objects.Advantages: This strategy is tolerant to high contention usage scenario, threads do not block each other.
Disadvantages: The sequence "read-clone-update-save" needs to allocate one object per each invocation of consumption method.
Usage recommendations: when you are not sure what kind of strategy is better for you.The
LocalBucketBuilder.build()without parameters uses this strategy.
-
SYNCHRONIZED
public static final SynchronizationStrategy SYNCHRONIZED
Blocking strategy based on javasynchronizedkeyword.Advantages: Never allocates memory.
Disadvantages: Thread which acquired the lock(and superseded from CPU by OS scheduler) can block another threads for significant time.
Usage recommendations: when your primary goal is avoiding of memory allocation and you do not care about contention.
-
NONE
public static final SynchronizationStrategy NONE
This is fake strategy which does not perform synchronization at all. It is usable when there are no multithreading access to same bucket, or synchronization already provided by third-party library or yourself.Advantages: Never allocates memory and never acquires any locks, in other words you pay nothing for synchronization.
Disadvantages: If your code or third-party library code has errors then bucket state will be corrupted.
Usage recommendations: iff you have guarantees that bucket will be never used from multiple threads, for example in cases where your third-party library(like akka or rx-java) prevents concurrent access and provide guarantees of visibility, or when you are so senior guy that can manage synchronization by yourself.
-
-
Method Detail
-
values
public static SynchronizationStrategy[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (SynchronizationStrategy c : SynchronizationStrategy.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static SynchronizationStrategy valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-
-