Ejemplo n.º 1
0
        static void AsyncAddSub(AddSubAction addSubAction)
        {
            WaitHandle[] waitHandles = new WaitHandle[]
            {
                new AutoResetEvent(false),
                new AutoResetEvent(false)
            };
            AddSub addSub  = new AddSub();
            Thread thread1 = new Thread(x =>
            {
                addSub.Adds(addSubAction);
                (waitHandles[0] as AutoResetEvent).Set();
            });
            Thread thread2 = new Thread(x =>
            {
                addSub.Subs(addSubAction);
                (waitHandles[1] as AutoResetEvent).Set();
            });

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            thread1.Start();
            thread2.Start();

            WaitHandle.WaitAll(waitHandles);
            stopwatch.Stop();
            Console.WriteLine($"Counter={AddSub.counter}, {stopwatch.ElapsedMilliseconds:N0}ms");
        }
Ejemplo n.º 2
0
        static void SyncAddSub()
        {
            AddSub    addSub    = new AddSub();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            addSub.Adds();
            addSub.Subs();
            stopwatch.Stop();
            Console.WriteLine($"Counter={AddSub.counter}, {stopwatch.ElapsedMilliseconds:N0}ms");
        }