Package org.apache.spark.util
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 ClassesModifier and TypeInterfaceDescriptionstatic final class
Final class representing a handle to a thread local value. -
Method Summary
-
Method Details
-
createHandle
-
get
scala.Option<T> get() -
set
-