JEP draft: Scope Locals
Mike Rettig
mike.rettig at gmail.com
Fri May 14 19:19:26 UTC 2021
I don't see a way to capture the lifecycle of the scoped variable. I think
for framework designers it's going to be important to know the life cycle
of the scoped variable especially with snapshotting and inheritance. The
lack of a defined life cycle for thread locals is one of the many reasons
why it should be avoided. If scoped locals have a well defined and
comprehensive life cycle then it will make working with them much easier
(especially when many threads are involved).
interface ScopedVariableLifecycle<T> {
ScopedVariable<T> begin();
}
interface ScopedVariable<T> {
T get();
void end();
Optional<ScopedVariableLifecycle<T>> createForChildScope();
Optional<ScopedVariableLifecycle<T>> createSnapshot();
}
static final ScopeLocal<MyType> x = ScopeLocal.forType(MyType.class);
//most scoped variables will probably use the standard singleton
implementations.
ScopedLocal.where(x, new Singleton(new MyType()));
ScopedLocal.where(x, new InheritableSingleton(new MyType()));
//a custom lifecycle (e.g. database connection that is opened on first
access and closed at the end of the scope).
ScopedLocal.where(x, new MyScopedVariableLifecycle());
On Wed, May 12, 2021 at 10:15 AM Andrew Haley <aph at redhat.com> wrote:
> There's been considerable discussion about scope locals on the loom-dev
> list,
> and it's now time to open this to a wider audience. This subject is
> important
> because. although scope locals were motivated by the the needs of Loom,
> they
> have many potential applications outside that project.
>
> The draft JEP is at
>
> https://bugs.openjdk.java.net/browse/JDK-8263012
>
> I've already received some very helpful suggestions for enhancements to
> the API, and it'll take me a while to work through them all. In particular,
> Paul Sandoz has suggested that I unify the classes Snapshot and Carrier,
> and it will take me some time to understand the consequences of that.
>
> In the meantime, please have a look at the JEP and comment here.
>
>
> For reference, earlier discussions are at
>
> https://mail.openjdk.java.net/pipermail/loom-dev/2021-March/002268.html
> https://mail.openjdk.java.net/pipermail/loom-dev/2021-April/002287.html
> https://mail.openjdk.java.net/pipermail/loom-dev/2021-May/002427.html
>
> --
> Andrew Haley (he/him)
> Java Platform Lead Engineer
> Red Hat UK Ltd. <https://www.redhat.com>
> https://keybase.io/andrewhaley
> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
>
>
>
More information about the loom-dev
mailing list