Advertisement
kikosiak

Untitled

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