kikosiak

Untitled

Dec 3rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. namespace zad5
  5. {
  6.     class Program
  7.     {
  8.         static int liczbaIteracji = 10;
  9.         static int liczbaWatkow = 10;
  10.         static void wyswietlajCos()
  11.         {
  12.             Console.WriteLine(Thread.CurrentThread.Name + " zaczyna wyświetlać liczby:");
  13.             Random rand = new Random();
  14.            for (int ii = 0; ii < liczbaIteracji; ii++)
  15.            {
  16.                 Console.Write("{0}, ", ii); //wyświetl liczbę
  17.                 Thread.Sleep(rand.Next(0, 2000)); //wstrzymaj działanie na czas od 0 do 2s
  18.             }
  19.             Console.WriteLine(Thread.CurrentThread.Name + " zakończył pisać liczby.");
  20.         }
  21.  
  22.         static void Wh()
  23.         {
  24.             Thread.SpinWait(100000000);
  25.         }
  26.        static void Th1()
  27.         {
  28.             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
  29.             var timerH = new Stopwatch();
  30.             timerH.Start();
  31.             Thread[] tabWatTh1 = new Thread[16];
  32.             for(int i = 0; i < 16; i++)
  33.             {
  34.                 tabWatTh1[i] = new Thread(Wh);
  35.                 tabWatTh1[i].Priority = ThreadPriority.Highest;
  36.                 tabWatTh1[i].Start();
  37.                
  38.             }
  39.             foreach (Thread th in tabWatTh1)
  40.             {
  41.                 th.Join();
  42.             }
  43.             timerH.Stop();
  44.             TimeSpan ts = timerH.Elapsed;
  45.             string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  46.             Console.WriteLine("\nTimer(H): " + elapsedTime + "\n");
  47.        }
  48.        static void Th2()
  49.        {
  50.            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
  51.            var timerL = new Stopwatch();
  52.            timerL.Start();
  53.            Thread[] tabWatTh2 = new Thread[16];
  54.             for (int i = 0; i < 16; i++)
  55.            {
  56.                 tabWatTh2[i] = new Thread(Wh);
  57.                 tabWatTh2[i].Priority = ThreadPriority.Lowest;
  58.                 tabWatTh2[i].Start();
  59.                
  60.             }
  61.             foreach (Thread th in tabWatTh2)
  62.             {
  63.                 th.Join();
  64.             }
  65.            timerL.Stop();
  66.            TimeSpan ts = timerL.Elapsed;
  67.            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
  68.            Console.WriteLine("\nTimer(L): " + elapsedTime+"\n");
  69.        }
  70.  
  71.         static void Main(string[] args)
  72.        {
  73.            Thread[] tablicaWatkow = new Thread[liczbaWatkow];
  74.            Thread watekH = new Thread(Th1);
  75.            Thread watekL = new Thread(Th2);
  76.            watekH.Start();
  77.            watekL.Start();
  78.  
  79.                watekH.Join();
  80.                watekL.Join();
  81.    
  82.            Console.WriteLine("To już jest koniec, naciśnij ENTER...");
  83.            Console.ReadLine();
  84.  
  85.         }
  86.     }
  87. }
Add Comment
Please, Sign In to add comment