c#全局静态数组锁定的问题
请问,c#中全局静态数组,如果被多个线程访问,是否每个线程访问时会把整个数组锁定?比如:数组array[],1号线程访问array[0]的内容,同时2号线程能否访问array[1]的内容?
--------------------编程问答-------------------- 默认不会锁的,只有手动的来加锁。 --------------------编程问答-------------------- 可以参考下面用法:
Array myArray = new int[] { 1, 2, 4 };
lock(myArray.SyncRoot)
{
foreach (Object item in myArray)
Console.WriteLine(item);
}
--------------------编程问答-------------------- 经测试,c#会自动锁定的,看来数组也跟普通对象一样。
请问有什么办法不让c#自动锁定一个数组呢? --------------------编程问答-------------------- 我把测试代码贴上,看看是不是我测试错了?
public partial class Form1 : Form
{
public static object[] oba = new object[2];
public Form1()
{
InitializeComponent();
oba[0] = new object();
oba[1] = new object();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread t1 = new Thread(new ThreadStart(accessoba1));
Thread t2 = new Thread(new ThreadStart(accessoba2));
t1.Start();
t2.Start();
}
private void accessoba1()
{
for (int i = 0; i < 1000000; i++)
{
DateTime dt1 = DateTime.Now;
oba[0].ToString();
TimeSpan ts = DateTime.Now - dt1;
if (ts.TotalMilliseconds > 0)
{
System.IO.File.AppendAllText("c:\\TestResult1.txt", ts.TotalMilliseconds.ToString() + Environment.NewLine);
}
}
}
private void accessoba2()
{
for (int i = 0; i < 1000000; i++)
{
DateTime dt1 = DateTime.Now;
oba[1].ToString();
TimeSpan ts = DateTime.Now - dt1;
if (ts.TotalMilliseconds > 0)
{
System.IO.File.AppendAllText("c:\\TestResult2.txt", ts.TotalMilliseconds.ToString() + Environment.NewLine);
}
}
} --------------------编程问答-------------------- 测试结果:
TestResult1.txt:
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
110.1584
10.0144
10.0144
10.0144
10.0144
110.1584
10.0144
10.0144
10.0144
10.0144
110.1584
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
130.1872
10.0144
20.0288
20.0288
10.0144
10.0144
10.0144
10.0144
20.0288
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
20.0288
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
10.0144
20.0288
10.0144
20.0288
50.072
10.0144
70.1008
10.0144
60.0864
60.0864
30.0432
10.0144
10.0144
--------------------编程问答-------------------- 你只是测了访问这个元素使用的时间,但是并不能说这个和锁有关。 --------------------编程问答-------------------- 你可以试试用1K个INT[] 和5个线程来做实验 让每个线程都循环輸出 INT[]的数组和线程的标识代号 你会发现 console輸出的是各个线程交叉着的 这表明 各个线程都能访问这个数组.... 假如console輸出是1个线程从头到尾一直輸出完 才到另一个线程从头到尾的輸出 那就是锁住了 --------------------编程问答-------------------- class Program
{
public static int Total = 10000000;
public static object[] obj = new object[Total];
static void Main(string[] args)
{
Thread[] trd = new Thread[2];
for (int i = 0; i < trd.Length; i++) {
trd[i] = new Thread(work);
trd[i].Start(i);
}
}
static void work(object i)
{
while (true) {
DateTime d1 = DateTime.MinValue;
DateTime d2 = DateTime.MinValue;
lock (obj.SyncRoot)//自己体会下有和没有lock的区别
{
d1 = System.DateTime.Now;
for (int j = 0; j < obj.Length; j++)
{
if (j % 2 == (int)i)
{
{
obj[j] = new object();
}
}
}
d2 = System.DateTime.Now;
}
TimeSpan ts = d2 - d1;
{
Console.WriteLine(string.Format("No.{0} work from {1} to {2} used {3}",
i, d1.ToString("HH:mm:ss ffff"), d2.ToString("HH:mm:ss ffff"), ts.TotalMilliseconds));
}
Console.ReadLine();
}
}
}
补充:.NET技术 , C#