Class SharedThreadContext<T extends Serializable>

java.lang.Object
org.ek9lang.core.SharedThreadContext<T>
All Implemented Interfaces:
Serializable, Consumer<Consumer<T>>

public class SharedThreadContext<T extends Serializable> extends Object implements Consumer<Consumer<T>>, Serializable
Concept here is to be able to protect raw java objects from multiple threaded access. These could be real full platform threads or the new virtualThreads from Java 19 onwards. We need a range of objects structures that are not synchronized (so access is quick) so we want a thread to effectively own the whole tree of data when it accesses it. This has been modeled on the 'Consumer' so hand your data over and then call 'accept' on this object with your own consumer, and you'll get data back (don't hold references to it). Keep it protected within this context.
See Also:
  • Constructor Details

    • SharedThreadContext

      public SharedThreadContext(T toBeProtected)
      Wraps the object being protected - hides it, so it can only be accessed via your consumer.
  • Method Details

    • accept

      public void accept(Consumer<T> consumer)
      Take ownership of the reentrant lock - wait if another thread has it. Once ownership is taken then your consumer 'accept' method will be called with your protected data as the parameter.
      Specified by:
      accept in interface Consumer<T extends Serializable>