ThreadStaticAttribute和ThreadLocal<T>使用区别总结
2011-04-15 16:39
671 查看
ThreadStaticAttribute和ThreadLocal<T>都是提供线程本地存储,ThreadLocal<T>是.net4才提供的。
ThreadStaticAttribute只能标注静态字段,而ThreadLocal<T>可以对实例字段才有效。
如果不标注ThreadStaticAttribute,则多个线程共享一个变量,这样的话会引起冲突的。
class Program
{
static void Main()
{
Console.WriteLine("use [ThreadStaticAttribute]");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData1.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use [ThreadStaticAttribute]");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData2.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use static member, use instance member");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread((new ThreadData3()).ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData4.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use [ThreadStaticAttribute], use ThreadLocal<int>");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData5.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use [ThreadStaticAttribute], use ThreadLocal<int>");
ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
{
return "Thread" + Thread.CurrentThread.ManagedThreadId;
});
// Action that prints out ThreadName for the current thread
Action action = () =>
{
// If ThreadName.IsValueCreated is true, it means that we are not the
// first action to run on this thread.
bool repeat = ThreadName.IsValueCreated;
Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : "");
};
// 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();
}
}
class ThreadData1
{
static int threadSpecificData;
public static void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData);
}
}
class ThreadData2
{
[ThreadStaticAttribute]//可¨¦以°?省º?去¨£¤Attribute直¡À接¨®写¡ä[ThreadStatic] 语®?法¤¡§糖¬?
static int threadSpecificData;
public static void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData);
}
}
class ThreadData3 //使º1用®?实º¦Ì例¤y方¤?法¤¡§也°2可¨¦以°?实º¦Ì现?上¦?述º?效¡ì果?,ê?每?个?线?程¨¬单Ì£¤独¨¤使º1用®?各¡Â自Á?的Ì?变À?量¢?
{
int threadSpecificData;
public void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData);
}
}
class ThreadData4
{
[ThreadStaticAttribute]//可¨¦以°?省º?去¨£¤Attribute直¡À接¨®写¡ä[ThreadStatic] 语®?法¤¡§糖¬?
static test t = null;
public static void ThreadStaticDemo()
{
t = new test();
Thread.Sleep(100);
// Display the static data.
Console.WriteLine(t.testvalue);
}
}
class test
{
public int testvalue = 0;
public test()
{
testvalue++;
}
}
class ThreadData5
{
static ThreadLocal<int> threadSpecificData = new ThreadLocal<int>();//only in .net framework 4.0. no need to use [ThreadStaticAttribute]
static public void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData.Value = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData.Value);
}
}
本文出自 “一只博客” 博客,请务必保留此出处http://cnn237111.blog.51cto.com/2359144/545801
ThreadStaticAttribute只能标注静态字段,而ThreadLocal<T>可以对实例字段才有效。
如果不标注ThreadStaticAttribute,则多个线程共享一个变量,这样的话会引起冲突的。
class Program
{
static void Main()
{
Console.WriteLine("use [ThreadStaticAttribute]");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData1.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use [ThreadStaticAttribute]");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData2.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use static member, use instance member");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread((new ThreadData3()).ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData4.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use [ThreadStaticAttribute], use ThreadLocal<int>");
for (int i = 0; i < 3; i++)
{
Thread newThread = new Thread(ThreadData5.ThreadStaticDemo);
newThread.Start();
}
Thread.Sleep(500);
Console.WriteLine("not use [ThreadStaticAttribute], use ThreadLocal<int>");
ThreadLocal<string> ThreadName = new ThreadLocal<string>(() =>
{
return "Thread" + Thread.CurrentThread.ManagedThreadId;
});
// Action that prints out ThreadName for the current thread
Action action = () =>
{
// If ThreadName.IsValueCreated is true, it means that we are not the
// first action to run on this thread.
bool repeat = ThreadName.IsValueCreated;
Console.WriteLine("ThreadName = {0} {1}", ThreadName.Value, repeat ? "(repeat)" : "");
};
// 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();
}
}
class ThreadData1
{
static int threadSpecificData;
public static void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData);
}
}
class ThreadData2
{
[ThreadStaticAttribute]//可¨¦以°?省º?去¨£¤Attribute直¡À接¨®写¡ä[ThreadStatic] 语®?法¤¡§糖¬?
static int threadSpecificData;
public static void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData);
}
}
class ThreadData3 //使º1用®?实º¦Ì例¤y方¤?法¤¡§也°2可¨¦以°?实º¦Ì现?上¦?述º?效¡ì果?,ê?每?个?线?程¨¬单Ì£¤独¨¤使º1用®?各¡Â自Á?的Ì?变À?量¢?
{
int threadSpecificData;
public void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData);
}
}
class ThreadData4
{
[ThreadStaticAttribute]//可¨¦以°?省º?去¨£¤Attribute直¡À接¨®写¡ä[ThreadStatic] 语®?法¤¡§糖¬?
static test t = null;
public static void ThreadStaticDemo()
{
t = new test();
Thread.Sleep(100);
// Display the static data.
Console.WriteLine(t.testvalue);
}
}
class test
{
public int testvalue = 0;
public test()
{
testvalue++;
}
}
class ThreadData5
{
static ThreadLocal<int> threadSpecificData = new ThreadLocal<int>();//only in .net framework 4.0. no need to use [ThreadStaticAttribute]
static public void ThreadStaticDemo()
{
// Store the managed thread id for each thread in the static
// variable.
threadSpecificData.Value = Thread.CurrentThread.ManagedThreadId;
// Allow other threads time to execute the same code, to show
// that the static data is unique to each thread.
Thread.Sleep(100);
// Display the static data.
Console.WriteLine("Data for managed thread {0}: {1}",
Thread.CurrentThread.ManagedThreadId, threadSpecificData.Value);
}
}
本文出自 “一只博客” 博客,请务必保留此出处http://cnn237111.blog.51cto.com/2359144/545801
相关文章推荐
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下
- 使用Threadlocal<T>的场景
- AsyncLocal<T>与ThreadLocal<T>区别研究
- Spring中<ref local=""/>与<ref bean=""/>区别
- 读<<CLR via C#>>总结(3) 值类型和引用类型的区别
- Spring中<ref local=""/>与<ref bean=""/>区别
- React 使用技巧总结 <二>
- 总结:#import和#include区别 #import和@class的区别 #import<>和#import""的区别
- JSP 使用<%@include%>报Duplicate local variable path 错误 解决方法
- ThreadLocal<T> Java线程局部变量
- ThreadLocal<T>的是否有设计问题
- 前端HTML<img>标签图像的属性和使用/图像img与背景background的区别
- 使用malloc函数时,#include<malloc.h>与#include<stdlib.h>的区别
- <jsp:include page="">和<%@ include file=""%>区别总结
- aapt使用,<uses-permission>和<permission>的区别
- 简单总结.NET中的各<% %>用法及区别
- linux >和>>的区别,<号使用
- <a href="#"><a herf="javascript:void(0)">...几种方法的使用与总结