Esempio n. 1
0
 public static void TestReader(ReadWriteLock readWriteLock)
 {
     readWriteLock.ReadLock();
     Thread.Sleep(500);
     //Console.WriteLine(Thread.CurrentThread.Name + "执行完毕");
     readWriteLock.ReadUnlock();
 }
Esempio n. 2
0
 public static void Add(ReadWriteLock readWriteLock)
 {
     for (int i = 0; i < 100000000; i++)
     {
         readWriteLock.WriteLock();
         N++;
         readWriteLock.WriteUnlock();
     }
 }
Esempio n. 3
0
 public static void TestReentrantWriter(ReadWriteLock readWriteLock)
 {
     readWriteLock.WriteLock();
     readWriteLock.WriteLock();
     readWriteLock.WriteLock();
     readWriteLock.WriteLock();
     Thread.Sleep(1000);
     readWriteLock.WriteUnlock();
     readWriteLock.WriteUnlock();
     readWriteLock.WriteUnlock();
     readWriteLock.WriteUnlock();
 }
Esempio n. 4
0
        static void CreateThread(bool share, int i, ReadWriteLock readWriteLock)
        {
            Thread thread;

            if (share)
            {
                thread      = new Thread(() => TestReader(readWriteLock));
                thread.Name = "Reader-" + i;
            }
            else
            {
                thread      = i == 5 ? new Thread(() => TestReentrantWriter(readWriteLock)) : new Thread(() => TestWriter(readWriteLock));
                thread.Name = "Writer-" + i;
            }
            thread.Start();
        }
Esempio n. 5
0
        static void TestLock()
        {
            var           start         = DateTime.Now;
            ReadWriteLock readWriteLock = new ReadWriteLock();
            Thread        thread1       = new Thread(() => Add(readWriteLock));
            Thread        thread2       = new Thread(() => Add(readWriteLock));

            thread1.Name = "thread1";
            thread2.Name = "thread2";
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();
            Console.WriteLine(number.GetNum());
            Console.WriteLine(DateTime.Now - start);
        }
Esempio n. 6
0
        public static void Main(string[] args)
        {
            ReadWriteLock readWriteLock = new ReadWriteLock();

            for (int i = 1; i <= 10; i++)
            {
                CreateThread(false, i, readWriteLock);
            }
            for (int i = 1; i <= 20; i++)
            {
                CreateThread(true, i, readWriteLock);
            }
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(500);
                readWriteLock.PrintQueue();
            }
            Console.ReadKey();
        }