Right, except each thread gets its own binding. So it's not necessarily that 
you'll get the value of the last binding up the call stack. This will only be 
true if you are in the same thread also.

I'm not sure if we agree on the rest, but explain it differently or not. 
ThreadLocal is an object, and so is a Clojure Var. When I say Clojure Var, I 
don't mean a variable, but an instance of the class clojure.lang.Var.

So you have two classes, ThreadLocal and Var. Each one gives you variable like 
behaviour. You'll need a true variable to point to the instance of each, in 
Clojure you can attach a local symbol to it, or a global symbol, same in java. 
In both, you'll probably want to store the instance through a globally 
accessible name, like with def in Clojure or a static in Java. You don't have 
too, but I don't see the use case for a local reference to the ThreadLocal or 
the Var.

So up to now, there's no difference between the two. Now, where the difference 
appears, is in their specific features, when working with the instances 
themselves. The ThreadLocal scope values by thread, and so does the Var. The 
only difference is that in Clojure, you can not set and get as you like, you 
must do it through a binding block, which will set on entry and unset on exit. 
In Java, you have to manually unset, and most probably should, as you exit.

Then there's the details, like Vars have a default global scope value, while 
ThreadLocal has a default init method if you get before a set.

The access scope to the Var instance or the ThreadLocal instance is up to you, 
put it in a local or a global variable, neither prevents you from doing either. 
The scope of the values returned by Var and ThreadLocal are both Thread scope, 
with Clojure adding an extra binding scope to Var on top of it.

I think we're saying the same thing at this point. In practice, you can think 
of ThreadLocal as thread scoped variables, and Vars as binding block scoped 
variables per thread.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to