1.5.3.3. fejezet, Szinkronizáció

Join

namespace DoSynch
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(delegate () { Thread.Sleep(2000); });
            t1.Start();
            t1.Join();
            Console.WriteLine("Vége");
 
            Thread t2 = new Thread(() => Thread.Sleep(2000));
            t2.Start();
            if (t2.Join(1000) == false)
            {
                Console.WriteLine("Az idő lejárt...");
                t2.Abort();  
                Console.ReadKey();
            }
        }
    }
}

locker

namespace DoSynch
{
    class TestLocker
    {
        static int x = 10;
        static int y = 20;
        static object locker = new object();
 
        static public void Divide()
        {
            lock (locker)
            {
                if (TestLocker.x != 0)
                {
                    Thread.Sleep(20);
                    Console.WriteLine("y/x = {0} ",TestLocker.y / TestLocker.x);
                    TestLocker.x = 0;
                }
                else
                {
                    Console.WriteLine("Divide by zero");
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Thread t3 = new Thread(new ThreadStart(TestLocker.Divide));
            Thread t4 = new Thread(new ThreadStart(TestLocker.Divide));
            t3.Start();
            t4.Start();
        }
    }
}

Mutex

namespace DoSynch
{
    class TestMutex
    {
        private Mutex mutex = new Mutex();
 
        public void ResourceMetod()
        {
            mutex.WaitOne();
            Console.WriteLine("{0} használja az erőforrást...", Thread.CurrentThread.Name);
            Thread.Sleep(1000);
            mutex.ReleaseMutex();
            Console.WriteLine("{0} elengedi az erőforrást...", Thread.CurrentThread.Name);
        }
    }
    class Program
    {
        static TestMutex tMutex = new TestMutex();
 
        static public void ResourceUserMethod()
        {
            for (int i = 0; i < 10; ++i)
            {
                tMutex.ResourceMetod();
            }
        }
 
        static void Main(string[] args)
        {
            List<Thread> threadList = new List<Thread>();
            for (int i = 0; i < 10; ++i)
            {
                threadList.Add(new Thread(new ThreadStart(Program.ResourceUserMethod))
                {
                    Name = "Thread" + i.ToString()
                });
            }
            threadList.ForEach((thread) => thread.Start());
 
            Console.ReadKey();
 
            threadList.ForEach((thread) => thread.Abort());
        }
    }
}

Semaphore

    class TestSemaphore
    {
        private static int initialConcurrentCount = 3;
        private static int maxConcurrentCount = 3;
        private Semaphore semaphore = new Semaphore(initialConcurrentCount, maxConcurrentCount);
        public void ResourceMetod()
        {
            semaphore.WaitOne();
            Console.WriteLine("{0} használja az erőforrást...", Thread.CurrentThread.Name);
            Thread.Sleep(1000);
            semaphore.Release();
            Console.WriteLine("{0} elengedi az erőforrást...", Thread.CurrentThread.Name);
        }
    }

Monitor

   class TestMonitor
    {
        public void ResourceMetod()
        {
            Monitor.Enter(this);
            Console.WriteLine("{0} használja az erõforrást...", Thread.CurrentThread.Name);
            Thread.Sleep(1000);
            Monitor.Exit(this);
            Console.WriteLine("{0} elengedi az erõforrást...", Thread.CurrentThread.Name);
        }
    }

lásd még:

  • ReaderWriterLock
  • ManualResetEvent
  • AutoResetEvent