static void Main(string[] args)
        {
            //********************************************************// Zadanie 2 - Liczenie z zamkiem w wielu wątkach

            Stopwatch     sw             = new Stopwatch();
            CountIntegral count_integral = new CountIntegral();

            Thread t1 = new Thread(new ParameterizedThreadStart(count_integral.Count));
            Thread t2 = new Thread(new ParameterizedThreadStart(count_integral.Count));
            Thread t3 = new Thread(new ParameterizedThreadStart(count_integral.Count));
            Thread t4 = new Thread(new ParameterizedThreadStart(count_integral.Count));

            sw.Start();
            t1.Start(new Interval(1, 10));
            t2.Start(new Interval(10, 22));
            t3.Start(new Interval(22, 33));
            t4.Start(new Interval(33, 40));

            t1.Join(); t2.Join(); t3.Join(); t4.Join();
            sw.Stop();

            Console.WriteLine("#2. Liczenie całki w 4 wątkach (z zamkiem): ");
            Console.WriteLine("\tElapsed milliseconds: " + sw.ElapsedMilliseconds);
            Console.WriteLine("\tWynik: " + Math.Round(CountIntegral.result, 2));
            CountIntegral.result = 0;
            sw.Reset();


            Console.ReadKey();
        }
Пример #2
0
        static void Main(string[] args)
        {
            CountIntegral count_integral = new CountIntegral();

            Stopwatch sw = new Stopwatch();

            //********************************************************// Liczenie w 1 wątku

            Console.WriteLine("Liczenie całki normalnie - w jednym wątku: ");
            sw.Start();
            count_integral.Count(new Interval(1, 40));
            sw.Stop();
            Console.WriteLine("\tElapsed milliseconds: " + sw.ElapsedMilliseconds);
            Console.WriteLine("\tWynik: " + Math.Round(CountIntegral.result, 2));
            CountIntegral.result = 0;
            sw.Reset();

            //********************************************************// Zadanie 1 - Liczenie bez zamka w wielu wątkach

            Thread t1 = new Thread(new ParameterizedThreadStart(count_integral.Count));
            Thread t2 = new Thread(new ParameterizedThreadStart(count_integral.Count));
            Thread t3 = new Thread(new ParameterizedThreadStart(count_integral.Count));
            Thread t4 = new Thread(new ParameterizedThreadStart(count_integral.Count));

            sw.Start();
            t1.Start(new Interval(1, 10));
            t2.Start(new Interval(10, 22));
            t3.Start(new Interval(22, 33));
            t4.Start(new Interval(33, 40));

            t1.Join(); t2.Join(); t3.Join(); t4.Join();
            sw.Stop();

            Console.WriteLine("\n#1. Liczenie całki w 4 wątkach (bez zamka): ");
            Console.WriteLine("\tElapsed milliseconds: " + sw.ElapsedMilliseconds);
            Console.WriteLine("\tWynik: " + Math.Round(CountIntegral.result, 2));
            CountIntegral.result = 0;
            sw.Reset();


            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            //********************************************************// Zadanie 3 - Zapisywanie wyników dla
            // poszczególnych przedziałów i na końcu w sekcji krytycznej sumowanie ich do wspólnej dla wszystkich
            // obiektów zmiennej statycznej.

            CountIntegral count_integral1 = new CountIntegral();
            CountIntegral count_integral2 = new CountIntegral();
            CountIntegral count_integral3 = new CountIntegral();
            CountIntegral count_integral4 = new CountIntegral();

            Thread t1 = new Thread(new ParameterizedThreadStart(count_integral1.Count));
            Thread t2 = new Thread(new ParameterizedThreadStart(count_integral2.Count));
            Thread t3 = new Thread(new ParameterizedThreadStart(count_integral3.Count));
            Thread t4 = new Thread(new ParameterizedThreadStart(count_integral4.Count));

            sw.Start();
            t1.Start(new Interval(1, 10));
            t2.Start(new Interval(10, 22));
            t3.Start(new Interval(22, 33));
            t4.Start(new Interval(33, 40));

            t1.Join(); t2.Join(); t3.Join(); t4.Join();
            sw.Stop();

            Console.WriteLine("#3. Liczenie całki w 4 wątkach (z zamkiem i lokalną zmienną dla" +
                              " każdego wątku): ");
            Console.WriteLine("\tElapsed milliseconds: " + sw.ElapsedMilliseconds);
            Console.WriteLine("\tWynik: " + Math.Round(CountIntegral.result, 2));
            sw.Reset();


            Console.ReadKey();
        }