In .net, there is an "uber" thread-local-storage (TLS) which allows arbitrary TLS data to auto-magically "jump" from one thread to another. It is based on the CallContext class.
In other words, a logical request can spawn a hierarchy of new threads - and each of those threads will have access to the same TLS of the original thread. It is a very powerful feature, particularly for logging, authorization, multi-tenancy, or branding concerns.
What is the equivalent in Java?
Only in .net 4.5 has the "logical callcontext" gained a "copy on write" capability that allows threads to make private modifications to the logical callcontext. In other words, .net is still maturing this capability and providing greater stability.
If Java has an equivalent notion, how stable is it? What issues does it have?
Clarification
I already know that Java has a thread local storage (TLS) capability. That is not the question. I am asking if Java has an equivalent of the .net "logical call context" which is a much more powerful construct than simple TLS.
C is a middle-level language as it binds the bridges between machine-level and high-level languages. Java is a high-level language as the translation of Java code takes place into machine language, using a compiler or interpreter. C is only compiled and not interpreted. Java is both compiled and interpreted.
As Java was inspired by C and C++, its syntax is similar to these languages. C++ is both a procedural and object-oriented programing language. Hence, C++ has features specific to procedural languages as well as features of object-oriented programming language. Java is a completely object-oriented programming language.
C is a compiled language that is it converts the code into machine language so that it could be understood by the machine or system. Java is an Interpreted language that is in Java, the code is first transformed into bytecode and that bytecode is then executed by the JVM (Java Virtual Machine).
There is no typedef mechanism in Java. You should definitely consider having a subclass for that purpose.
Maybe InheritableThreadLocal
is what you're looking for?
I'm not sure if it's exactly the same, but as far as I understand it meets this requirement:
a logical request can spawn a hierarchy of new threads - and each of those threads will have access to the same TLS of the original thread.
From the docs
This class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values. Normally the child's values will be identical to the parent's; however, the child's value can be made an arbitrary function of the parent's by overriding the childValue method in this class. Inheritable thread-local variables are used in preference to ordinary thread-local variables when the per-thread-attribute being maintained in the variable (e.g., User ID, Transaction ID) must be automatically transmitted to any child threads that are created.
I don't know about the "copy on write" capability you mentioned, but I guess you can override InheritableThreadLocal.childValue(T)
to proxy the parent's value so that writes don't go through to the parent and modify the current thread's local storage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With