Thread Synchronization public class Sample Class private object
Thread Synchronization שלב א – ללא תהליך נפרד : לדוגמה public class Sample. Class { משאב משותף private object thread. Lock = new object(); private int num; מתודת עבודה private Random rnd = new Random(); public void Print. Numbers() { Console. Write. Line("-> {0} is executing Print. Numbers()", Thread. Current. Thread. Name); Console. Write("Numbers: "); for (int i = 0; i < 10; i++) שינוי המשאב { המשותף Thread. Sleep(100 * rnd. Next(5)); num = i; static void Main(string[] args) Console. Write("{0}, ", num); { } Thread. Current. Thread. Name = "Main Thread"; Console. Write. Line(); Sample. Class p = new Sample. Class(); } //1 - Run method 10 time } Console. Foreground. Color = Console. Color. Red; for (int i = 0; i < 10; i++) { p. Print. Numbers(); הפעלת } המתודה } Thread. Problem : קוד דוגמה
Thread Synchronization : פלט שלב א
Thread Synchronization } public class Sample. Class שלב ב – בתהליך נפרד : לדוגמה { משאב משותף private object thread. Lock = new object(); private int num; private Random rnd = new Random(); מתודת עבודה public void Print. Numbers. Asynch () { Console. Write. Line("-> {0} is executing Print. Numbers()", Thread. Current. Thread. Name); Console. Write("Numbers: "); for (int i = 0; i < 10; i++) שינוי המשאב { המשותף Thread. Sleep(100 * rnd. Next(5)); num = i; static void Main(string[] args) Console. Write("{0}, ", num); { } Console. Foreground. Color = Console. Color. Green; Console. Write. Line(); Thread[] threads_arr 1 = new Thread[10]; יצירת for (int i = 0; i < 10; i++) } { } תהליכים threads_arr 1[i] = new Thread (new Thread. Start(p. Print. Numbers. Asynch)); threads_arr 1[i]. Name = string. Format("Worker thread #{0}", i); } foreach (Thread t in threads_arr 1) t. Start(); הפעלת המתודה
Thread Synchronization : פלט שלב ב
Thread Synchronization Blocking. Simple Blocking או Blocking § השיטה הפשוטה ביותר לסינכרון נקראת : ( בשני מקרים Blocking) § תהליך נמצא במצב של חסימה . – חסימת התהליך לפרק זמן מוגדר Thread. Sleep(…) § הפעלת המתודה . חסימת התהליך הנוכחי עד לסיומו של תהליך אחר - Thread. Join() § הפעלת המתודה static void Main(string[] args). CPU הוא לא מקבל הקצאת , § בזמן שתהליך נמצא במצב חסום { התהליך הראשי ממתין עד Thread thread = new Thread(Do. Something); thread לסיומו של התהליך thread. Start(); Console. Write. Line(thread. Thread. State); public static void Do. Something() thread. Join(); { של Blocking Console. Write. Line(thread. Thread. State); int counter=0; Console. Write. Line("Application Ended successfully"); שניות 2 while (counter<10) } { Thread. Sleep(2000); Console. Write. Line(Date. Time. Now. To. Long. Time. String()); counter++; } } Thread. Blocking : קוד דוגמה
Thread Synchronization – דוגמה נוספת Join התהליך הראשי תלוי בתוצאת שני , מתבצע חישוב מסויים בשני תהליכים נפרדים : התהליכים static decimal num 1 = 1; static decimal num 2 = 1; static Random rnd = new Random(); static void Worker. Thread 1() { var watch = Stopwatch. Start. New(); num 1 = Fibonacci(rnd. Next(10, 30)); watch. Stop(); Console. Write. Line("From Worker. Thread 1 : num 1 = " + num 1 + " , Working time: " + watch. Elapsed. Milliseconds + " ms"); } static void Worker. Thread 2() { var watch = Stopwatch. Start. New(); num 2=Fibonacci(rnd. Next(10, 30)); watch. Stop(); Console. Write. Line("From Worker. Thread 2 : num 2 = " + num 2 + " , Working time: " + watch. Elapsed. Milliseconds + " ms"); } Thread. Join. Sample : קוד דוגמה
Thread Synchronization ( – דוגמה נוספת )המשך Join static void Main(string[] args) { Thread t 1 = new Thread(new Thread. Start(Worker. Thread 1)); t 1. Start(); Thread t 2 = new Thread(new Thread. Start(Worker. Thread 2)); t 2. Start(); t 1. Join(); t 2. Join(); Console. Write. Line(num 1 + num 2); } המתנה לסיום שני התהליכים חישוב התלוי בתוצאת שני התהליכים Thread. Join. Sample : קוד דוגמה
Thread Synchronization Critical Section class Program { static void Main(string[] args) { Sample sample = new Sample(); Thread thread = new Thread(sample. Do. Task 1); thread. Start(); sample. Do. Task 1(); } } class Sample { private bool flag; public void Do. Task 1() { if (!flag) { flag = true; Console. Write. Line("Done - " + Thread. Current. Thread. Name); } } Critical. Section. Problem 01 : דוגמת קוד } : § מה יהיה הפלט של הקוד הבא
Thread Synchronization ? למה Critical. Section. Problem 01 : דוגמת קוד
Thread Synchronization class Program { static void Main(string[] args) { Sample sample = new Sample(); Thread thread = new Thread(sample. Do. Task 2); thread. Start(); sample. Do. Task 2(); } } class Sample { private bool flag; public void Do. Task 2() { if (!flag) { Console. Write. Line("Done"); flag = true; } } } Critical. Section. Problem 01 : דוגמת קוד : ומה יהיה הפלט של הקוד הבא
Thread Synchronization ? § למה
Thread Synchronization class Program { : להיגרם מהקוד הבא static void Main(string[] args) { Thread. Unsafe sample = new Thread. Unsafe(); Thread thread = new Thread(sample. Do. Task); thread. Start(); sample. Do. Task(); } } class Thread. Unsafe { private int num 1=10, num 2=5; public void Do. Task() { if (num 2 != 0) { Console. Write. Line(num 1 / num 2); } Critical. Section. Problem 02 : דוגמת קוד num 2 = 0; } מה הסכנה העלולה
Thread Synchronization class Program { static void Main(string[] args) { Thread. Unsafe sample = new Thread. Unsafe(); Thread thread = new Thread(sample. Do. Task); thread. Start(); sample. Do. Task(); אובייקט } נעילה / הסנכרון } class Thread. Safe { private int num 1=10, num 2=5; private object lock. This = new object(); נעילה public void Do. Task() { lock (lock. This) { if (num 2 != 0) { Console. Write. Line(num 1 / num 2); } num 2 = 0; } } } Lock. Sample 01 : דוגמת קוד Lock מנגנון נעילה הפשוט ביותר למקטעים . קריטיים , מבטיח גישה אקסקלוסיבית למקטע קריטי רק תהליך אחד יוכל לגשת למקטע הקריטי . בו בעת ? למה : הפלט
Thread Synchronization public static void Do. Something 01() { lock (locker) { for (int i = 0; i < 1000; i++) { Console. Foreground. Color = Console. Color. Red; Console. Write("X"); } } } public static void Do. Something 02() { lock (locker) { for (int i = 0; i < 1000; i++) { Console. Foreground. Color = Console. Color. Green; Console. Write("Y"); } } } . § הפעם הנעילה מתבצעת על שתי מתודות אינו מאפשר lock § אובייקט שננעל באמצעות גישה גם מדובר על מתודה אחרת או אפילו . מחלקה אחרת Lock. Sample 02 : דוגמת קוד
Thread Synchronization – תהליך ראשון ממתין לתהליך שני שישחרר את החסימה בעוד שהתהליך Deadlock. השני ממתין לסיום החסימה בתהליך הראשון public void Func 1() { lock (lock 1) { Console. Write. Line("Func 1 -> lock 1 activated"); Calc 1(); } } public void Calc 1() { lock (lock 2) { Console. Write. Line("Func 1 -> lock 2 activated"); shared_num++; Console. Write. Line("From Func 1: {0})", shared_num); } } Deadlock. Sample 01 : דוגמת קוד
Thread Synchronization § המשך public void Func 2() { lock (lock 2) { Console. Write. Line("Func 2 -> lock 2 activated"); Calc 2(); } } public void Calc 2() { lock (lock 1) { Console. Write. Line("Func 2 -> lock 1 activated"); shared_num++; Console. Write. Line("From Func 2: {0})", shared_num); } } Deadlock. Sample 01 : דוגמת קוד
Thread Synchronization class Thread. Safe { private int num 1 = 10, num 2 = 5; private object locker = new object(); public void Do. Task() {. חריגה Monitor. Enter(locker); if (num 2 != 0) { Console. Write. Line(num 1 / num 2); } num 2 = 0; Monitor. Exit(locker); } } Monitor היא סה"כ קיצור למחלקה lock המילה השמורה . Int 32 - ו int כמו תעורר Monitor. Enter- מבלי לקרוא ל Monitor. Exit- קריאה ל Monitor. Sample : דוגמת קוד
- Slides: 30