what is new - .NET 4.5: review of the ThreadLocal
2013-01-24 00:00
495 查看
ThreadLocal is a managed impl of the windows thread local storage, basically with thread local, only the creator of the thread local variable can see/save/load values of the thread local variables. It is a means of isolation. Let's first examine an example of the
ThreadLocal<T> class
A threadLocal storage will be created when it is first used. it is one time task for each thread. Below is an adapted code with my own comments in.
ThreadLocal<T> class
A threadLocal storage will be created when it is first used. it is one time task for each thread. Below is an adapted code with my own comments in.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ThreadLocalStorage { // boqwang: // in this example, we will going to examing the thread-local storage as it is supported by the CLR runtime. class Program { static void Main(string[] args) { ThreadLocal<string> ThreadName = new ThreadLocal<string>(() => // initialize the thread local storage with the { return "Threadi" + Thread.CurrentThread.ManagedThreadId; }); Action action = () => { bool repeat = ThreadName.IsValueCreated; // you can check to see if the ThreadLocal variable is created, for threadlocal, only the creator can see it. Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : ""); // repeat here means we are running on the same thread which creates the thread local storage... }; // Launch eight of them. On 4 cores or less, you should see some repeat ThreadNames Parallel.Invoke(action, action, action, action, action, action, action, action); // Dispose when you are done ThreadName.Dispose(); // -- remember to deallocate if you are done with it. .. } } }the output is something as follow.
ThreadName = Threadi4 ThreadName = Threadi4 (repeat) ThreadName = Threadi4 (repeat) ThreadName = Threadi3 ThreadName = Threadi3 (repeat) ThreadName = Threadi3 (repeat) ThreadName = Threadi3 (repeat) ThreadName = Threadi4 (repeat)
相关文章推荐
- what is the mean of thread safe ?线程安全是什么意思呢?
- 软件随想录(local.joelonsoftware.com/wiki)-2001年05月05日 这个国家的狗做什么工作? - What Is the Work of Dogs in this Cou
- 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
- [Chapter 3 Process]Practice 3.4 Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets.
- what is new - .net 4.5: Simple zip examples
- android.util.AndroidRuntimeException Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? com.uethinking.microvideo.manag
- The usage of the Javascript method call and apply(what is the new and object.create difference.)(What does call(null) mean?)
- what is the hell of uv's tiling and offset
- what is the purpose of using translatable in Android strings?
- What is the fastest way of (not) logging?
- php 报错Deprecated: Assigning the return value of new by reference is deprecated in的解决办法
- what is the location of the directory of c header files that match your running kernel -SLES11.2
- Multi-thread: What is the difference between OpenMP and MPI
- What do you think is the role of parents in preventing crime amongst young people?
- What is blocker on the road of learning English?
- 欧拉项目 Problem 12 of What is the value of the first triangle number to have over five hundred divisors
- What is the location of the directory of C header files that match
- What is the difference between a process and a thread?
- What is the use of the res/values/public.xml file on Android?
- What is the location of the directory of C header files that match your running kernel?