Thread Local: A Convenient Abomination. 69
ThreadLocal variables are a wonderfully convenient way to associate data with a given thread. Indeed, frameworks like Hibernate take advantage of this to hold session information. However, the practice depends upon assumption that a thread is equivalent to a unit-of-work. This is a faulty assumption
Thirteen years ago, while working on my first book, Jim Coplien and I were having a debate on the nature of threads and objects. He made a clarifying statement that has stuck with me since. He said: “An object is an abstraction of function. A thread is an abstraction of schedule.”
It has become the norm, in Java applications, to assume that there is a one-to-one correspondence between a thread, and a unit-of-work. This appears to make sense since every Servlet request has it’s own particular thread. Framework authors have built on this assumption by putting unit-of-work related information (e.g. session) into ThreadLocal variables.
Clearly, the more ThreadLocal variables that hold unit-of-work related information, the more that the thread and the unit-of-work are related. While very convenient, the basic assumption is dead wrong.
A unit-of-work is a task to be performed. There is no rule that says that this task must be single threaded. Nor is there any rule that says that the components of the task must run at the same priority. Indeed, it is not uncommon for a task to have a high-priority event driven part, and a low priority compute-bound part.
Consider, for example, a unit-of-work that makes a request from a third-party-service, and then processes the response with a complex number-crunching algorithm. We want the request to run at a high priority so that it is not waiting for other low priority compute-bound tasks to finish. On the other hand, we want the number-cruncher to run at a low priority so that it yields the processor as soon as possible whenever one of the high priority requests needs a few cycles.
Clearly these two tasks form a complete unit-of-work, but just as clearly they should run as two different threads—probably with a queue in between them in a standard producer-consumer model.
Where are the unit-of-work related variables? They can’t be kept in a ThreadLocal since each part of the task runs in a separate thread. They can’t be kept in static variables since there is more than one thread. The answer is that they have to be passed around between the threads as function arguments on the stack, and recorded in the data structured placed on the queue.
So, though convenient, ThreadLocal variables confuse the issue of separating function from schedule. They tempt us to couple function and schedule together. This is unfortunate since the correspondence of function and schedule is weak and accidental.
What we’d really like is to be able to create UnitOfWorkLocal variables.
Do you know of any programs or frameworks which have “UnitOfWorkLocal variables”?
Surely ThreadLocal storage of an unit of work probably isn’t the best choice. But it’s at least totally valid, where you know a unit of work doesn’t span multiple threads. With this assumption in mind, holding the unit of work in a thread local variable spares you from passing around the unit of work to everyone who needs it.
I think what really can be precluded in most cases is, that multiple units of work are used in the same thread at the same time (there just may be nested units of work). That means, that nothing keeps you from letting threads “inherit” a unit of work from other threads. So thread local storage definitly isn’t generally wrong, is it?
Since actually you tought us a long time ago to use processes and not necessarily data as the model for our objects, then I guess that another name of UnitOfWorkLocal variables are “members” :-)
Corba offers two ways of context (=session) propagation: Thread bound context propagation and custom context propagation. The latter means that it’s up to you passing a context or session object around or have a ThreadLocal do the job. I think that the user of a library should be given the choice. However, not using ThreadLocalS in the library code doesn’t hinder the user to implement it anyway. Forcing the user to deal with ThreadLocalS that linger around behind the scenes is definetly a smell (similar to mutable static members, aka globals in the library’s back yard).
From the desig standpoint you are perfectly right not to recommend ThreadLocalS. They rise a lot of problems such as testability issues and make code less understandable.
From a pragmatic standpoint I have to admit that I used them successfully to overcome shortcommings in third party APIs. They also helped me to reestablish transaction safety in lousy legacy applications.
My personal recommendation: As long as there is no urgent need, avoid them.
I like your idea of a UnitOfWorkLocal variables. They could be implemented as something similar to ThreadLocals but they should be automatically inherited by subthreads and somehow a worker thread should be able to attach to a UnitOfWorkLocal variable.
Code-wise I see something like this (invented in approximately 1 minute, so it’s not very thought out):This looks like it should be simple to implement with a stack of ThreadLocals (except that if we assume that the above is Java, it would require language extensions), but I guess I just haven’t thought about it enough to see the devil in the details. What is nice about the idea is that it seems like a alternative to ThreadLocals, singletons and globals that maps better to the problem domain.
Or perhaps I’m just jetlagged… :-)
Im a german Student and my english isnt that good. Could be that i am aiming at a wrong spot, but as far as i understand the issue, i would agree with Bob.
Of course it is a lot of more work seperating data from functions. And i dont really have a clue what difficulties can occur but in my naive student being i would say the following:
Considering the fact, that multithreading becomes even more important than in the past, this could be a very interesting way to accelerate applications. I could imagine using data and functions seperately to balance the load that a thread must work on. Depending on how the implementation of hardware is, a multicore processer could share the work in differnt amount of pieces. Doing this by each function working parallel on that data, all tasks are divided into the maximum count of processors the hardware can offer or functions the application need to be done.
My first entry
@Martin, my immediate thought was the same: just create subthreads that will inherit the thread-local values. However, I think, this approach will not work thread-pooled environments (e.g. in application servers).
Will ThreadGroups help somehow? Can a variables be stored in ThreadGroup objects. Perhaps, we should start pooling threads at the group level instead?
The thread pool problem is what I meant with “a worker thread should be able to attach to a UnitOfWorkLocal variable”. In general, the solution seems simple. Just create a map that maps threads to units of work, like
map<Thread, UnitOfWork> dataMap
. The problem is just that the code to access a UnitOfWorkLocal variable will be a bit convoluted. I guess the Ruby folks would happily implement something like this transparently, but in Java the code to call a function on a UnitOfWorkLocal would be along the lines of:dataMap.get(Thread.currentThread()).getSession().setFoo(1)
(where the Session is the UnitOfWorkLocal)instead of just
session.setFoo(1)
(if session was a static variable).Implementation details aside, I think UnitOfWorkLocal is an interesting idiom. One could think of it as static variables for a unit of work instead of for the whole process. Or one could think of it as ThreadLocals for a unit of work instead of for just one thread.
A UnitOfWorkLocal variable is close enough to a session variable for most practical purposes. You just need to ensure that the session gets passed around across threads, asynchronous operations, callbacks, and other such things.
There are mechanisms for doing this. In .Net you can store session variables in the ExecutionContext. Queued work items on the thread pool acquire the execution context of the caller. You can also explicitly enter an execution context for a delimited region of a thread’s lifetime. You can use that to ensure that session data remains in scope across boundary transitions. In fact it can go a good deal further than that.
Many web frameworks provide a simpler mechanism in the form of an HttpContext for tracking the current session. You can often leverage the fact that the framework is already doing the work of passing this object around. That’s often enough to provide robust storage and scoping for UnitOfWorkLocals (and you can adopt more complex mechanisms when necessary).
I think it’s a bad idea for people writing business logic for business applications to dabble in threads. Problems of concurrency are best left to the database; finding concurrency primitives in business logic is imho a bad smell.
So there’s a good reason for assuming thread==unit of work. It makes business applications simpler… and more robust.
“It has become the norm, in Java applications, to assume that there is a one-to-one correspondence between a thread, and a unit-of-work.”
I wonder where you are deducing this from: it’s wrong. In my view, “it has become the norm, in java applications” to assume that resources must come from a “scope”: this is what frameworks such as Spring and Seam induce the programmers to think. A “scope”, then, may be anything convenient for your architecture, ranging from the more or less extreme approaches of a “singleton” and a “prototype” (in Spring terms).
A “thread local” scope is a possibility, such as a “web session” scope, or a “flow scope”: this, is what is actually becoming “the norm”, and it’s not far from being able to declare a “unitofwork” variable.
It may be an abomination to always use a ThreadLocal as a UoW “session” variable, but if you design an application based on the fact that evry UoW will be conducted inside its own Thread, then you can thank the JVM devs for providing the feature.
In other words, ThreadLocal is not an abomination per-say, its how people use it that make it an abomination… Isn’t it?
Drop the unit of work with a session token onto a queue and then when the UoW is excuted create a session from the token details and then use InheritableThreadLocal instances to store details…when execution finishes you can then store the result onto a result queue to use later or return to the user!?
That said…Thread locals cause problems with NIO since you need to do some sort of context switching with the UoW data…
JRocket was a good implementation of a JVM until they removed the pseudo threading code…basically one thread could be made to act like multiple threads because at the end of the day a JVM is a virtual machine so you can have virtual threads meaning less context switching!? Going off topic abit but the reason people want to move away from ThreadLocals is that representing UoW with a link to threads will, at some point, slow down the number of user requests per second…moving away from thread locals will, at some point, make your code run quicker once true NIO solutions come into play…
Its “convenient” to use InheritableThreadLocals all said and done but it may not be the best scalable solution, however hardware is becoming cheaper and clustering is another solution to the number of requests per second problem!?
InheritableThreadLocals all said and done but it may not be the best scalable solution, however hardware is becoming cheaper and clustering is another s
Clearly, the more ThreadLocal variables that hold unit-of-work related information, the more that the thread and the unit-of-work are related. While very convenient, the basic assumption is dead wrong.
Cool!Thanks for ur nice sharing!!It help me a lot with those information!!
In buying any of LV EPI Leather, it is noteworthy to do careful examination on its design components and the signature materials used. That is to make sure you are not buying a fake counterpart of LV products. As you can see, the monogramming on the LV bag must be placed in a symmetrical arrangement to ensure its authenticity. The stitching on all LV bags are firm and smooth. For multi-colored designs, you also have to check if the colors are included in the list of the 33 colors officially utilized by Louis Vuitton Bags.
The very few deserving endorsers include Jennifer Lopez and Madonna. The brands popularity is so strong that it is mentioned in hit songs; most notable of them was the one from Kanye West.
I built a castle in the swamp and it sunk. I built a second castle and it sunk too. I built a third castle and it burned down and then sunk.
They are different. When you want to know. When I come to here, I think I am in the right place. the web gives me a lot of infomation, it is very informative. I think lots of people can learn much here. I will come to here again. Thanks.
Mold making is the core business of Intertech (Taiwan). With world level technology, Intertech enjoys a very good reputation for making Injection Mold and Plastic Moldsfor their worldwide customers.
They are different. When you want to know. When I come to here, I think I am in the right place. the web gives me a lot of infomation, it is very informative. I think lots of people can learn much here. I will come to here again. Thanks.
inside fact your imaginative writing abilities receive inspired me to start my confess blog currently. trulythe blogging is spreading its wings speedily. your note along is a desirable example of it.
Clearly these two tasks form a complete unit-of-work, but just as clearly they should run as two different threads—probably with a queue in between them in a standard producer-consumer model.
Many web frameworks provide a simpler mechanism in the form of an HttpContext for tracking the current session. You can often leverage the fact that the framework is already doing the work of passing this object around.
“comments”site:blog.objectmentor.com
Welcome to PDF to ePub Converter
The PDF to ePub converter is a perfect and professional software to help you convert PDF to ePub. Meanwhile the PDF to ePub Converter enables you to convert PDF to ePub and ePub to PDF. Many kinds of formats such as HTML, XML, TEXT, Gif, JPEG can be converted by this powerful PDF to ePub converter. And you can use this PDF to ePub Converter to parse PDF file(include all text, image) and rebuild it. Then you will get the perfect output ePub files.
By the way, the another PDF to EPUB Converter,after conversion by it you can enjoy the eBooks with iPad, iPhone, iPod Touch, Sony Reader, iRex Digital Reader 1000, PocketBook Reader and so on. Meanwhile, the converter suppports converting in batches and with the intuitive and clear interface, it is very easy even for the beginner. So you can have a try of this excellent PDF to EPUB converter. It will benefit you a lot!
We are the professional jacket manufacturer, jacket supplier, jacket factory, welcome you to custom jacket.
At fashion-world4u you find Imitation
To be, or not to be- that is a question.Whether ipad bag tis nobler in the mind to suffer The slings and Game Controllers arrows of outrageous fortune Or to take arms against a sea of troubles, And USB Gadgets by opposing end them.66666666666
Tremendously educational many thanks, I reckon your trusty readers would definitely want even more writing like this carry on the excellent hard work.
interesting issue …
internette görüntülü olarak okey oyunu oyna, gerçek kisilerle tanis, turnuva heyecanini yasa.
This research I developed the quantitative reasoning.
These christian louboutin patent leather pumps are just the same as selling in the franchise store.Or even better. Quite cheap christian louboutin leather platform pumps.I could say this is the best one I have ever ordered online.
I found that his foot odors never bring us to death.I never regret buying these beats by dr dre studio for him. These beats by dr dre solo are just the same as selling in the franchise store.Or even better.
Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with, Online UK costume and fashion jewellery shop with,
Thanks for the good post…
Looking for more post and it’s really useful to me….
shop good for me
Cool stuff. Icon generator rocks! Thanks for these links.
it needs a bokmark so i can come back to it later ,nice stuff
Kinda cerebral for me)) You’re too clever i guess…
with the intuitive and clear interface, it is very easy even for the beginner. So you can have a try of this excellent PDF to EPUB converter. It will benefit you a lo
with the intuitive and clear interface, it is very easy even for the beginner. So you can have a try of this excellent PDF to EPUB converter. It will benefit you a lobeats by dre sale cheap beats by dre
ok
I really Mechanical Engineering Job Description love this articles and you Chief Executive Officer Job Description can read those articles that Desktop Support Job Description to very much attractive for the info Police Officer Job Description You can never forget this info and improve your knowledge in this blog
i go dhis
Ich mag dieses Artikels, sind besonders gut.
Berkeley had a liberal element in the student body who tended to be quite active. I think that’s in general a feature of intellectually active places.
The professional design make you foot more comfortable. Even more tantalizing,this pattern make your legs look as long as you can,it will make you looked more attractive.Moveover,it has reasonable price.If you are a popular woman,do not miss it.
Technical details of Christian Louboutin Velours Scrunch Suede Boots Coffee:
Fashion, delicate, luxurious Christian louboutins shoes on sale, one of its series is Christian Louboutin Tall Boots, is urbanism collocation. This Christian louboutins shoes design makes people new and refreshing. Red soles shoes is personality, your charm will be wonderful performance.
Berkeley had a liberal element in the student body who tended to be quite active. I think that’s in general a feature of intellectually active places.
A university studentbeats by dr dre caught by the enemy, the enemy tied him at the poles,just beats solo headphones purple and then asked him: say, where are you? You do not say it electrocuted! Scheap dr.dre beats studio headphones balck/yellowtudents back to the enemy a word, the result was electrocuted, he said: I am TVU.Hot sale beats by dr dre pro headphones
MTS to MOV Converter is a powerful and easy-to-use MPEG to AVI converter, which enables you to effortlessly convert MTS to AVI, MTS to MPEG, MTS to MP4, MTS to 3GP, MTS to WMV, MTS to RM, MTS to RMVB, MTS to FLV, MTS to VCD/SVCD/DVD, MTS to iPod MP4, MTS to PSP MP4 and so forth with high quality. It supports adding watermarks,subtitles to the videos. MTS to MP4 MTS to WMV MTS to MPG
Most of us will delete the SMS file if the iPhone inbox is full. For some of the very important text file, you would like to save it to Mac hard drive and read it later or you need print them. So, why not export the text message to HDD and save it now?
I love this article, it’s very well.Oakley Scalpel sunglasses
Berkeley had a liberal element in the student body who tended to be quite active. I think that’s in general a feature of intellectually active places.
The Charms Fit Leather Pandora Bracelet collection offers a rich array of stylish Pandora Like Charm Bracelets created by artisans using the finest materials.
Realy it’s quite intresting post.I like it. Dirty Showst
Thread Local: A Convenient Abomination. 59 hoo,good article!!I like the post!41
Thread Local: A Convenient Abomination. 60 good post162
replica Oakley Antix Sunglasses replica Oakley Batwolf Sunglasses replica Oakley C Six Sunglasses replica Oakley Commit SQ Sunglasses
Among women, there are certain colors that are Christian Louboutin shoes for less never out of fashion and one such most significant Christian Louboutin bibi pumps color is the color red. Women all around the Christian Louboutin globe love wearing red color, which is why shoes by Christian Louboutin in some cultures (especially in the east) red is the color of the bride.
If you think this will solve the problem for good? Let’s get ready to support research …
A Convenient Abomination. 59 hoo,good article!!I like the post!41
Did you know that there are multiple kinds of gifts that you can give to your partner? You can give dozens and dozens of different kinds without ever repeating any single one! Smart people know if you start the process of shopping knowing what kind of gifts are available you will be able to find what you are looking for much easier!Sometimes the kind you give is in the way you give it! The “surprise gift” is a very popular one and doesn’t have to be anything truly material?
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one has its own, independently initialized copy of the variable. The instances are typically private static fields in classes that wish to associate state with a thread.
This is an affecting point of view on this topic. I am happy you shared your ideas and I find myself agreeing
Discount prada shoes are very important for virtually every male in her way of life. Prada America’s Cup sneakers are essential for numerous requirements.This happens to most shoes pc-489-1”>prada men sneakers & -pc-490-1”>prada high top sneaker that don’t offer any lace protection, though. Prada Shoulder Bags is every popular on the market .