if (i % 25 != 0)
{
var t = Task.Factory.StartNew(ReadSomething_lock);
lstTask.Add(t);
}
else
{
var t = Task.Factory.StartNew(WriteSomething_lock);
lstTask.Add(t);
}
}
Task.WaitAll(lstTask.ToArray());
sw.Stop();
Console.WriteLine("使用lock方式,耗时:" + sw.Elapsed);
}
static private object _lock1 = new object();
static public void ReadSomething_lock()
{
lock (_lock1)
{
//Console.WriteLine("{0} Thread ID {1} reading sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode());
Thread.Sleep(10);//模拟读取信息
//Console.WriteLine("{0} Thread ID {1} reading end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode());
}
}
static public void WriteSomething_lock()
{
lock (_lock1)
{
//Console.WriteLine("{0} Thread ID {1} writing sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode());
Thread.Sleep(100);//模拟写入信息
//Console.WriteLine("{0} Thread ID {1} writing end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode());
}
}
static public void ReadSomething()
{
rwl.EnterReadLock();
try
{
//Console.WriteLine("{0} Thread ID {1} reading sth...", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode());
Thread.Sleep(10);//模拟读取信息
//Console.WriteLine("{0} Thread ID {1} reading end.", DateTime.Now.ToString("hh:mm:ss fff"), Thread.CurrentThread.GetHashCode());
}
finally
{
rwl.ExitReadLock();
}
}
static public void WriteSomething()
{
rwl.EnterWriteLock();
try