类 Shared<T>
Shared Object Pooling
A Shared Object at the simplest represents an object that can perform basic atomicity operations, such as modifying a non-final variable inside a lambda.However, the main quirk of a Shared Object is that all allocated SharedObjects are placed inside a pool. Similar to Java's String pool, if multiple objects have the same value, only one instance of it will exist in that pool, and one pointer. This method is not meant to save on memory, nor does it even achieve such a goal; instead, it is focused on making sure multiple instances of an object that can hold the same value don't exist at the same time. For example, in concurrency, Shared objects will help to maintain atomicity and mitigate race conditions.
The Shared Class itself does not hold the true value of the object, instead a singular
ID or numerical pointer to the value in the pool. Furthermore, the usage of this class
is very very simple and acts just like a normal Wrapper class that just exists to get
rid of the "modification of non-final variable in a lambda" error thrown by Java. Just
look at setValue(Object)
and getValue()
they can't be anymore easier!
All the internal complexity are hidden away
Example Usage
public record FooBar(int integerValue,String stringValue) {} // ... SharedfooBar=Shared.of(new FooBar(30,"Hello World")); fooBar.setValue(new FooBar(100,"Foo!")); System.out.println(fooBar.getValue().stringValue());
- 作者:
- Jack Meng
-
字段概要
-
构造器概要
-
方法概要
修饰符和类型方法说明static <V extends Shared<?>>
booleancomputeEquality
(V a, V b) boolean
void
getValue()
Please note that this method can return a nullable object because of garbage collectionWeakReference
static <E> Shared<E>
of
(E value) Creator function used to return a new shared object handler.void
Modifies the current value pointed toprivate static <T> WeakReference<T>
weakOf
(T obj)
-
字段详细资料
-
OBJECTS
-
id
private long id
-
-
构造器详细资料
-
Shared
Creates a new Shared Object pool with default initializations, mainly hooking up this Shared object to the pool.- 参数:
e
- The initial value
-
-
方法详细资料
-
of
Creator function used to return a new shared object handler.- 类型参数:
E
- Type of the value- 参数:
value
- The value- 返回:
- The Shared object handler
-
weakOf
-
setValue
Modifies the current value pointed to- 参数:
e
- value pointed to
-
expirePoolReserve
public void expirePoolReserve() -
getValue
Please note that this method can return a nullable object because of garbage collectionWeakReference
- 返回:
- The shared object
-
computeEquality
-
equals
-