Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- namespace zad5
- {
- class Program
- {
- static int liczbaIteracji = 10;
- static int liczbaWatkow = 10;
- static void wyswietlajCos()
- {
- Console.WriteLine(Thread.CurrentThread.Name + " zaczyna wyświetlać liczby:");
- Random rand = new Random();
- for (int ii = 0; ii < liczbaIteracji; ii++)
- {
- Console.Write("{0}, ", ii); //wyświetl liczbę
- Thread.Sleep(rand.Next(0, 2000)); //wstrzymaj działanie na czas od 0 do 2s
- }
- Console.WriteLine(Thread.CurrentThread.Name + " zakończył pisać liczby.");
- }
- static void Wh()
- {
- Thread.SpinWait(100000000);
- }
- static void Th1()
- {
- Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
- var timerH = new Stopwatch();
- timerH.Start();
- Thread[] tabWatTh1 = new Thread[16];
- for(int i = 0; i < 16; i++)
- {
- tabWatTh1[i] = new Thread(Wh);
- tabWatTh1[i].Priority = ThreadPriority.Highest;
- tabWatTh1[i].Start();
- }
- foreach (Thread th in tabWatTh1)
- {
- th.Join();
- }
- timerH.Stop();
- TimeSpan ts = timerH.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
- Console.WriteLine("\nTimer(H): " + elapsedTime + "\n");
- }
- static void Th2()
- {
- Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
- var timerL = new Stopwatch();
- timerL.Start();
- Thread[] tabWatTh2 = new Thread[16];
- for (int i = 0; i < 16; i++)
- {
- tabWatTh2[i] = new Thread(Wh);
- tabWatTh2[i].Priority = ThreadPriority.Lowest;
- tabWatTh2[i].Start();
- }
- foreach (Thread th in tabWatTh2)
- {
- th.Join();
- }
- timerL.Stop();
- TimeSpan ts = timerL.Elapsed;
- string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
- Console.WriteLine("\nTimer(L): " + elapsedTime+"\n");
- }
- static void Main(string[] args)
- {
- Thread[] tablicaWatkow = new Thread[liczbaWatkow];
- Thread watekH = new Thread(Th1);
- Thread watekL = new Thread(Th2);
- watekH.Start();
- watekL.Start();
- watekH.Join();
- watekL.Join();
- Console.WriteLine("To już jest koniec, naciśnij ENTER...");
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement