Ejemplo n.º 1
0
        private static void SimpleSyncLock()
        {
            Thread tOne   = new Thread(SimpleSyncThread);
            Thread tTwo   = new Thread(SimpleSyncThread);
            Thread tThree = new Thread(SimpleSyncThread);

            SimpleSyncParams tOneParams = new SimpleSyncParams()
            {
                AppendThis = "1",
                Count      = 3
            };
            SimpleSyncParams tTwoParams = new SimpleSyncParams()
            {
                AppendThis = "2",
                Count      = 3
            };
            SimpleSyncParams tThreeParams = new SimpleSyncParams()
            {
                AppendThis = "3",
                Count      = 3
            };

            tOne.Start(tOneParams);
            tTwo.Start(tTwoParams);
            tThree.Start(tThreeParams);

            tOne.Join();
            tTwo.Join();

            Console.WriteLine();
        }
Ejemplo n.º 2
0
        private static void SimpleSyncThread(object args)
        {
            Random           rnd     = new Random();
            SimpleSyncParams tParams = args as SimpleSyncParams;

            for (int i = 0; i < 10; ++i)
            {
                lock (simpleLock)
                {
                    for (int c = 0; c < tParams.Count; ++c)
                    {
                        Console.Write(tParams.AppendThis);  // appending next piece
                        Thread.Sleep(rnd.Next(1, 5) * 100); // waiting some random time to simulate irregularity
                    }
                }
            }
        }