private void DLTest1(string lk = "duyanming") { try { Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss:ffff} {System.Threading.Thread.CurrentThread.ManagedThreadId} DLTest1拉取锁({lk})"); using (DLock dLock = new DLock(lk, 10000)) { Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss:ffff} {System.Threading.Thread.CurrentThread.ManagedThreadId} DLTest1进入锁({lk})"); System.Threading.Thread.Sleep(50); } Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss:ffff} {System.Threading.Thread.CurrentThread.ManagedThreadId} DLTest1离开锁({lk})"); } catch (Exception e) { Console.WriteLine(e.Message); } }
public void Handle() { #region RedisHelper 预热初始化资源 Init(); RedisHelper.Set("Anno", 0); var rlt = RedisHelper.Get <int>("Anno"); #endregion List <Task> tasks = new List <Task>(); for (int i = 0; i < 10000; i++) { tasks.Add(Task.Run(() => { using (DLock dLock = new DLock("Anno")) { var rlt = RedisHelper.Get <int>("Anno"); RedisHelper.Set("Anno", ++rlt); } })); } Task.WaitAll(tasks.ToArray()); var endRlt = RedisHelper.Get <int>("Anno"); Console.WriteLine(endRlt); }