Interface LexicalThreadLocal<T>


public interface LexicalThreadLocal<T>
Helper trait for defining thread locals with lexical scoping. With this helper, the thread local is private and can only be set by the LexicalThreadLocal.Handle. The LexicalThreadLocal.Handle only exposes the thread local value to functions passed into its runWith method. This pattern allows for the lifetime of the thread local value to be strictly controlled.

Rather than calling tl.set(...) and tl.remove() you would get a handle and execute your code in handle.runWith { ... }.

Example:


   object Credentials extends LexicalThreadLocal[Int] {
     def create(creds: Map[String, String]) = new Handle(Some(creds))
   }
   ...
   val handle = Credentials.create(Map("key" -> "value"))
   assert(Credentials.get() == None)
   handle.runWith {
     assert(Credentials.get() == Some(Map("key" -> "value")))
   }
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Final class representing a handle to a thread local value.
  • Method Summary

    Modifier and Type
    Method
    Description
    createHandle(scala.Option<T> opt)
     
    scala.Option<T>
    get()
     
    void
    set(scala.Option<T> opt)
     
  • Method Details