You are on page 1of 60

‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬

‫حل أسئلة نظم تشغيل عملي‬

‫األستاذ\ نبيل العمدي‬

‫إعداد اللجنة العلمية \‬


‫تقنية معلومات مستوى ثاني‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Answer the following questions:
:‫اجب عن األسئلة التالية‬
1\Can you start a thread twice?
‫ ألي ثريد مرتين؟‬Start ‫ هل يمكن أن نعمل‬:‫السؤال‬
:‫ والمثال التالي يوضح ذلك‬.‫ لنفس الثريد مرتين‬Start ‫ ال يمكن أن نعمل‬:‫اإلجابة‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void print ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started ");
Console.WriteLine(t.Name+"Sleep for one Second\n ");
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(print);
t1.Name = "Thread 1";
t1.Start();
//‫البرنامج فأن التالي السطر عن التعليق ازلنا إذا السطر هذا في‬
//‫) الدالة استدعاء في خطاء حدوث الى ويشير يتنفذ لن‬start( ‫مرتين‬
//t1.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
‫ الثانية فإن البرنامج لن يتنفذ‬t.Start ‫إذا ازلنا التعليق عن السطر‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫?‪2\What is difference between process and thread‬‬
‫السؤال‪ :‬ما االختالف بين العملية والثريد؟‬
‫الجواب‪ :‬االختالف هو ان العملية تحتكر موارد الحاسوب من ذاكرة ومعالج لها فقط وال تسمح‬
‫بتشاركها مع العمليات األخرى على خالف الثريد الذي يسمح بتشارك الموارد من ذاكرة ومعالج‬
‫وكذلك يسمح بتشارك وقت التنفيذ كذلك يمكن للعملية أن تحتوي على أكثر من ثريد بينما الثريد‬
‫ال ينفذ اال عملية واحدة‪.‬‬

‫‪3\What is the difference between foreground and‬‬


‫?‪background threads‬‬
‫السؤال‪ :‬ما االختالف بين ال ‪ Foreground Thread‬و ال ‪Background Thread‬‬
‫الجواب‪ :‬االختالف هو أن ‪ Foreground Thread‬يستمر في التنفيذ حتى إنهاء جميع‬
‫عملياته وال يتوقف حتى إذا أكمل الثريد الرئيسي عمله) هذا يعني أن دورة حياة ال‬
‫‪ Foreground Thread‬غير مرتبطة بالثريد الرئيسي)‪.‬‬
‫اما ال ‪ Background Thread‬فهو عكس ال ‪ Foreground Thread‬أي أن الثريد سوف‬
‫يتوقف عن التنفيذ عند إنها عمل الثريد الرئيسي حتى لو لم يكمل الثريد عمله) هذا يعني أن‬
‫دورة حياة ال ‪ Background Thread‬تعتمد على دورة حياة الثريد الرئيسي(‪.‬‬

‫المثال التالي يوضح الفرق بينهما وكيف نتحكم بنوع الثريد‪:‬‬


‫نتحكم بنوع الثريد عن طريق الخاصية ‪ IsBackground‬فإذا كانت قيمة الخاصية ‪true‬‬
‫يكون نوع الثريد ‪ Background‬وإذا كانت قيمة الخاصية ‪ false‬يكون نوع الثريد‬
‫‪foreground‬‬
‫مالحظة‪ - :‬يمكن االستغناء في المثال عن السطر ‪t2.IsBackground= false‬‬
‫الن نوع الثريد افتراضيا يكون ‪.foreground‬‬
‫;‪using System‬‬
‫;‪using System.Threading‬‬
‫‪namespace threading‬‬
‫{‬
‫‪class Program‬‬
‫{‬
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
public static void BackgroundThread()
{
Console.WriteLine(Thread.CurrentThread.Name+" is started");
for (int i = 0; i < 40; i++)
{
Console.WriteLine(i*i);
Thread.Sleep(200);
}
}
public static void ForegroundThread()
{
Console.WriteLine(Thread.CurrentThread.Name+" is started");
for (int i = 0; i < 40; i++)
{
Console.WriteLine(i * i);
Thread.Sleep(200);
}
}
static void Main(string[] args)
{
Console.WriteLine("Main Thread Started");
Thread t1 = new Thread(BackgroundThread);
t1.IsBackground = true;
t1.Name = "Thread 1";
Thread t2 = new Thread(BackgroundThread);
t2.IsBackground = false;
t2.Name = "Thread 2";
t1.Start();
t2.Start();
Console.WriteLine("Main Thread finish");
Console.ReadKey();
}
}
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
4\What are the main properties of thread class?
:‫ ماهي الخصائص الرئيسية لفصيل الثريد؟ الجواب‬:‫السؤال‬
1- Current Thread.
2-IsBackground.
3-Name.
4-Priority.
5-Threadstare.

5\What are the methods used in thread class?

:‫ ماهي الدوال الرئيسية لفصيل الثريد؟ الجواب‬:‫السؤال‬


1-Abort.
2-Join.
3-ResetAbort.
4-Start.
5-Sleep.
6-Stop.
7-Suspend.
8-Resum.

6\What are the methods used in monitor class ?


‫؟‬Monitor ‫ ماهي الدوال المستخدمة في الفصيل‬:‫السؤال‬
‫ لتحرير‬Monitor.Exit ‫ لإلقفال والدالة‬Monitor.Enter ‫ الدوال المستخدمة هي‬:‫الجواب‬
.‫اإلقفال‬
7\What are the methods used in mutex class ?

‫؟‬Mutex ‫ ماهي الدوال المستخدمة في الفصيل‬:‫السؤال‬


Mutex.ReleaseMutex ‫ لإلقفال والدالة‬Mutex.Waitone ‫ الدوال المستخدمة هي‬:‫الجواب‬
.‫لتحرير اإلقفال‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫‪8\What are the advantages and disadvantages of‬‬
‫?‪multithreading‬‬
‫السؤال‪ :‬ماهي المميزات والعيوب لتعدد الثريدات؟‬
‫المميزات‪-:‬‬

‫‪ -1‬الثريدات تحقق توازي حقيقي في بيئة متعددة المعالجات‪ ،‬هذا يعني أن الثريدات يمكن‬
‫أن تعمل أكثر من عملية بشكل متوازي‪.‬‬
‫‪ -2‬زيادة استجابة واجهة المستخدم‪ ،‬وذلك الن الثريد الرئيسي يستمر في معالجة احداث‬
‫لوحة المفاتيح والماوس والثريدات الفرعية تقوم بعمليات أخرى‪.‬‬
‫‪ -3‬تحقق الثريدات استخدام أفضل لوحدة المعالجة المركزية‪ ،‬أي أنه عندما ينتظر أحد‬
‫الثريدات لالستجابة من العتاد المادي او أي حدث يقوم الثريد االخر باالستمرار في‬
‫التنفيذ‪.‬‬
‫‪ -4‬تمييز المهام ذات األولويات المختلفة‪ ،‬أي أن الثريد األعلى أولوية ينفذ المهمة األكثر‬
‫أهمية والثريد األقل أولوية ينفذ المهمة األقل أهمية‪.‬‬
‫العيوب‪-:‬‬

‫‪ -1‬يمكن أن يكون النظام معقد وصعب الفهم‪.‬‬


‫‪ -2‬تكاليف وحدة المعالجة المركزية – الجدولة – واإلدارة – اإلنشاء – والتبديل التي‬
‫تحتاجها الثريدات قد تجعل النظام بطيء بدال من جعله أسرع‪.‬‬
‫‪ -3‬السيطرة على وصول الموارد المشتركة وذلك عن طريق استخدام أساليب المزامنة‬
‫مثل االقفال وإذا لم يتم التزامن بشكل صحيح فقد يؤدي ذلك الي مشاكل مثل االقفال‬
‫المميت‪ ،‬وهذه المشكلة قد يصعب حلها‪.‬‬
‫‪ -4‬اختبار التطبيقات متعددة الثريدات صعب جدا‪ ،‬وذلك الن العيوب تكون مرتبطة‬
‫بالتوقيت والتداخل في تنفيذ الثريدات وهذا قد يؤدي الى نتائج غير متوقعة‪.‬‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬

‫? ‪9\What is monitor class in c#‬‬


‫السؤال‪ :‬ماذا يكون الفصيل ‪ Monitor‬في لغة السي شارب؟‬
‫الجواب‪ :‬هو نوع من أنواع الفصائل التي توفر دوال ألجل االقفال ودوال لتحرير االقفال‬
‫ودوال أخرى وهو يشبه ‪ lock‬لكنه يختلف على ال ‪ lock‬بوجود دوال لعملية االقفال ودوال‬
‫لتحرير اإلقفال بينما ال توجد دوال في ال ‪lock‬‬

‫? ‪10\What is mutex class in c#‬‬


‫السؤال‪ :‬ماذا يكون الفصيل ‪ Mutex‬في لغة السي شارب؟‬
‫الجواب‪ :‬هو نوع من أنواع الفصائل التي توفر دوال ألجل االقفال ودوال لتحرير االقفال‬
‫ودوال أخرى وهو يشبه ‪ lock‬ولكنه يمكن أن يعمل عبر عمليات متعددة بمعنى اخر يمكن‬
‫أن يكون إقفال ال ‪ Mutex‬على مستوى الكمبيوتر وعلى مستوى التطبيق وهذه تعتبر ميزة‬
‫ليست موجودة في ال ‪ .lock‬وأيضا بوجود دوال لإلقفال ودوال لتحرير اإلقفال‪.‬‬
‫? ‪11\How can we scheduled a thread in c#‬‬
‫السؤال‪ :‬كيف يمكننا جدولة الثريد في السي شارب؟‬
‫الجواب‪ :‬يمكن أن نقوم بجدولة الثريدات وذلك عن طريق استخدام األولويات أي أن الثريد‬
‫األعلى أولوية ينفذ أوال ثم ينفذ الثريد األقل أولوية وهكذا‪ .‬حل السؤال ‪ 12‬يوضح ذلك‪...‬‬

‫?‪12\What are the priority value used for scheduling a thread in c#‬‬
‫السؤال‪ :‬ماهي قيم األولويات المستخدمة في جدولة الثريدات؟‬
‫الجواب‪ :‬هنالك خمس قيم او خمس أولويات للقيام بعملية الجدولة للثريدات وهي‪:‬‬
‫‪1- Highest:- take the value 4‬‬
‫‪2- Above Normal:- take the value 3‬‬
‫‪3- Normal:- take the value 2‬‬
‫‪4- Below Normal:- take the value 1‬‬
‫‪5- Lowest:- tack the value 0‬‬
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
‫ ويمكن تغيير أولوية الثريد عن طريق‬Normal ‫القيمة االفتراضية لثريد عند إنشاءه هي‬
-:‫ بالطريقة التالية‬Priority ‫استخدام الخاصية‬
T1.Priority = ThreadPriority.Highest;
.‫ هو أعلى أولوية من باقي الثريدات‬Highest ‫والثريد الذي يمتلك القيمة‬
:‫ولطباعة قيمة أولوية الثريد نستخدم الطريقة التالي‬
Console.WriteLine(T1.Priority(;
.Highest ‫ناتج السطر البرمجي هي طباعة القيمة‬
.Priority ‫والمثال التالي يوضح طريقة استخدام الخاصية‬
.Normal ‫ لم نعطي له أولوية لذلك سوف يحتفظ بالقيمة االفتراضية وهي‬t5 ‫الحظ ان الثريد‬
using System;
using System.Threading;
namespace priorityThread
{
class Program
{
public static void fun1()
{
Console.WriteLine(Thread.CurrentThread.Name+" is started");
Console.WriteLine(Thread.CurrentThread.Name+" has Priority
" +Thread.CurrentThread.Priority+"\n");
Thread.Sleep(200);
}
static void Main(string[] args)
{
Console.WriteLine("Main Thread Started");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun1);
Thread t3 = new Thread(fun1);
Thread t4 = new Thread(fun1);
Thread t5 = new Thread(fun1);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t3.Name = "Thread 3";
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
t4.Name = "Thread 4";
t5.Name = "Thread 5";

t1.Priority = ThreadPriority.Highest;
t2.Priority = ThreadPriority.AboveNormal;
t3.Priority = ThreadPriority.BelowNormal;
t4.Priority = ThreadPriority.Lowest;

t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
t5.Join();

Console.WriteLine("Main Thread finish");


Console.ReadKey();
}
}
}

13\What are the two types of thread in c# ?


‫ ماهي أنواع الثريدات الموجودة في السي شارب؟‬:‫السؤال‬
.Foreground Thread ‫ و‬Background Thread ‫ يوجد نوعان هما‬:‫الجواب‬
14\What are the syntax for creating and starting a thread
in c#?
‫ ماهي البنية او التركيب ألجل إنشاء وبدء الثريد في السي شارب؟‬:‫السؤال‬
System. Threading ‫ تضمين المكتبة‬- :‫ أوال‬:‫الجواب‬
Start ‫ ونعمل له تنفيذ عن طريق الدالة‬Thread ‫ثم نقوم بعمل غرض من الفصيل‬
Thread t = new Thread( ‫ ;)اسم الدالة‬t.Start();
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫? ‪15\What is the namespace used for multithreading in c#‬‬
‫السؤال‪ :‬ما هو فضاء األسماء المستخدم ألجل الثريدات المتعددة في السي شارب؟ الجواب‪:‬‬
‫;‪System.Threading‬‬

‫?‪16\What is The difference Between Monitor.enter And Monitor.exit‬‬

‫السؤال‪ :‬ما هو الفرق بين الدالة ‪ Monitor.enter‬والدالة ‪Monitor.exit‬؟‬


‫الجواب‪ :‬الفرق هو أن الدالة ‪ Monitor.enter‬تعمل على إقفال الغرض إي تدل على بداية‬
‫المنطقة الحرجة (بداية اإلقفال) أما الدالة ‪ Monitor.exit‬تعمل على تحرير القفل إي‬
‫تحرير المورد المقفل عليها ليستخدمها ثريد اخر‪.‬‬

‫?‪17\What is The difference Between Start and abort‬‬

‫السؤال‪ :‬ما هو االختالف بين الدالتين ‪Start and Abort‬؟‬


‫الجواب‪ :‬الدالة ‪ Start‬تستخدم لبدء عمل الثريد اما الدالة ‪ Abort‬فتعمل على إحباط عمل‬
‫الثريد (إيقاف عمل الثريد) حتى لو لم يكمل تنفيذه وال يمكن عمل ‪ Start‬مره أخرى لثريد تم‬
‫إحباطه‪.‬‬

‫? ‪18\What is The difference Between Suspend and Sleep‬‬

‫السؤال‪ :‬ما هو االختالف بين الدالتين ‪Suspend and Sleep‬؟‬


‫الجواب‪ :‬الدالة ‪ Suspend‬تقوم بإيقاف عمل الثريد حتى يتم عمل له استئناف التنفيذ عن‬
‫طريق الدالة ‪ ،Resume‬اما الدالة ‪ Sleep‬فتعمل على إيقاف عمل الثريد لفتة محددة من‬
‫الزمن تقدر بالملي ثانية‪.‬‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬

‫? ‪19\ What is The difference Between Join And Sleep‬‬

‫السؤال‪ :‬ما هو االختالف بين الدالتين ‪Sleep and Join‬؟‬


‫الجواب‪ :‬الدالة ‪ Join‬تعمل على إيقاف عمل جميع الثريدات عن العمل حتى يكمل الثريد‬
‫الذي استدعاها عمله‪ ،‬اما الدالة ‪ Sleep‬فتقوم بإيقاف عمل الثريد الذي استدعاها لمدة محددة‬
‫من الزمن تقدر بالملي ثانية‪.‬‬

‫‪20\ What is The difference and Similarity between‬‬

‫?‪method/function Join(time) and property IsAlive‬‬


‫السؤال‪ :‬ما هو االختالف والتشابه بين الدالة (‪ Join (time‬والخاصية ‪IsAlive‬؟‬
‫الجواب‪ :‬التشابه هو أن كل منهما تعيد القمة ‪ True‬او القيمة ‪ False‬واالختالف في طريقة‬
‫العمل‪.‬‬
‫حيث أن الدالة ‪ Join‬تعيد القمة ‪ True‬إذا انها الثريد عملة خالل الفترة المحددة في الدالة‬
‫وتعيد القيمة ‪ False‬إذا لم ينهي الثريد عمله في الفترة المحدودة‪.‬‬
‫اما الخاصية ‪ IsAlive‬فهي تعيد القيمة ‪ True‬إذا ما زال الثريد فعال (قيد التنفيذ) وتعيد‬
‫القيمة ‪ False‬إذا كان الثريد غير فعال إي إذا أنهى عمله‪.‬‬

‫? ‪21\ Between Join And Sleep‬‬


‫السؤال‪ :‬ما هو االختالف والتشابه بين الدالتين ‪ Join‬و‪Sleep‬؟‬
‫الجواب‪ :‬التشابه هو أن الدالتين تستخدمان إليقاف عمل الثريدات‪.‬‬
‫واالختالف في طريقة العمل حيث أن الدالة ‪ join‬تعمل على إيقاف عمل جميع الثريدات‬
‫حتى يكمل الثريد الذي استدعاها عمله‪ ،‬اما الدالة ‪ Sleep‬فتعمل على إيقاف عمل الثريد الذي‬
‫استدعاها لفترة محددة من الزمن تقدر بالملي ثانية‪.‬‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫?‪22\ What is the meaning of mutex‬‬

‫السؤال‪ :‬ماذا يكون معنى ال ‪Mutex‬؟‬


‫الجواب‪ :‬هو نوع من أنواع الفصائل التي توفر دوال ألجل االقفال ودوال لتحرير اإلقفال‬
‫وهو يشبه ‪ lock‬ولكنه يمكن أن يعمل عبر عمليات متعددة بمعنى اخر يمكن أن يكون إقفال‬
‫ال ‪ Mutex‬على مستوى الكمبيوتر وعلى مستوى التطبيق‪.‬‬

‫‪23\ Explain why and how a deadlock can occur in‬‬


‫?‪multithreading with an example‬‬
‫السؤال‪ :‬أشرح ما هو سبب وكيفية حدوث اإلقفال المميت في تعدد الثريدات مع مثال؟‬
‫الجواب‪ :‬االقفال المميت يحدث عندما يكون اثنان من الثريدات او أكثر كل واحد منهما‬
‫منتظر للموارد المقفل عليها من قبل الثريد االخر بحيث ال يمكن ألي منها اكمال عمله‪.‬‬

‫?‪24\ What is multi threading‬‬


‫السؤال‪ :‬ماذا يعني تعدد الثريدات؟‬
‫الجواب‪ :‬تعني تنفيذ عدد من الثريدات في نفس الوقت ألداء مهام متعددة في وقت واحد‪.‬‬

‫‪25\ Can you count some names of synchronization‬‬


‫?‪primitives‬‬

‫تم حله مسبقا‬


‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫?‪26 \ What is synchronization and why it is important‬‬

‫السؤال‪ :‬ماذا هو التزامن ولماذا هو مهم؟‬


‫الجواب‪ :‬يشير مصطلح المزامنة او التزامن الى تنسيق احداث الثريدات ألجل الحصول‬
‫على نتائج يمكن التنبؤ بها والتزامن يكون مهم بشكل خاص عندما تكون مجموعة من‬
‫الثريدات لها قدرة الوصول الى نفس البيانات‪.‬‬

‫?‪27\ What is the meaning of deadlocks‬‬


‫تم حله مسبقا‬

‫‪28\ What is the difference between monitor and lock in‬‬


‫?‪C#‬‬

‫تم حله مسبقا‬

‫? ‪29\ During a thread's lifetime, what states can it have‬‬

‫السؤال‪ :‬من خالل دروة حياة الثريد‪ ،‬ما هي الحاالت الممكنة للثريد؟ الجواب‪:‬‬
‫‪1- Unstarted‬‬
‫‪2- Running‬‬
‫‪3- WaitSleepJoin‬‬
‫‪4- Suspended‬‬
‫‪5- Aborted‬‬
‫‪6- Stopped‬‬
‫?‪30\ What is time slicing‬‬
‫السؤال‪ :‬ماذا يكون تقطيع الوقت؟‬
‫الجواب‪ :‬هي تقنية يستخدمها نظام التشغيل لجدولة الثريدات حيث يقوم بتوفير مدة قصيرة‬
‫قد تكون ثالث ثواني مثال لكل ثريد‪ ،‬ويجعل الثريدات تعمل بالتناوب على ذلك الوقت‪ ،‬أي‬
‫مثال لدينا اثنان من الثريدات رقم ‪ 1‬واخر رقم ‪ 2‬يقوم األول بتنفيذ جزء من المهمة في‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫ظرف ال‪ 3‬ثواني ثم عندما ينقضي الوقت يتوقف ليقوم الثاني بالجزء الخاص به في نفس‬
‫المدة التي حددها المجدول وهكذا كل ثريد يتوفر على وقت قصير يقوم بجزء من المهمة‬
‫فقط حتى ينهي االثنان مهمتهما كاملة‪.‬‬

‫‪31\ Why might you describe thread behavior as‬‬


‫?‪unpredictable‬‬
‫السؤال‪ :‬لماذا قد تصف سلوك الثريد بأنه ال يمكن التنبؤ به؟‬
‫الجواب‪ :‬يمكننا أن نقول إن سلوك الثريد ال يمكن التنبؤ به بسبب أن تنفيذ الثريد يعتمد‬
‫على مجدول الثريدات‪ .‬وكل نظام تشغيل لدية مجدول يختلف عن النظام االخر‪.‬‬
‫?‪32\ What are the states associated with the thread‬‬
‫السؤال‪ :‬ماذا تكون الحاالت المرتبطة بالثريد؟‬
‫الجواب‪ :‬هنالك عدة حاالت يمر بها الثريد خالل دورة حياته وهي كتالي‪- :‬‬
‫‪1- The Unstarted State:‬‬
‫هذه الحالة تكون عندما يكون الثريد قد تم إنشاءه لكن لم يتم استدعاء الدالة ‪ Start‬ليبدأ‬
‫التنفيذ‪.‬‬
‫)اسم الدالة( ‪Thread t = new Thread‬‬
‫هذا السطر يدل انه قد تم إنشاء الثريد لكنه لن يبدا العمل حتى يتم استدعاء الدالة‬
‫‪ Start‬كتالي‪:‬‬
‫;)(‪t.Start‬‬

‫‪2- The Ready State:‬‬


‫هذه الحالة تكون عندما يكون الثريد قد تم إنشاءه وايضا تم استدعاء الدالة ‪ Start‬لكنه منتظر‬
‫لدورة المعالج‪.‬‬

‫‪3- The Not Runnable State:‬‬


‫هذه الحالة عندما يكون الثريد غير قابل للتنفيذ وذلك عند استدعاء أحد الدوال التالية‪:‬‬
‫‪ Sleep‬او ‪ Wait‬او يحظر تنفيذ الثريد من قبل عمليات االدخال واإلخراج‪.‬‬
‫تم مراجعته من قبل‪ :‬أ‪ /‬أكـــرم عبدالكريم لقــمان ‪773133872 ‬‬
‫?‪33\ What is context-switching in multi-threading‬‬
‫السؤال‪ :‬ماذا يكون تبديل السياق في الثريدات المتعددة؟‬
‫الجواب‪ :‬يقصد بتبديل السياق عملية تبديل وقت التنفيذ بين الثريدات بحيث يظهر أن كل ثريد‬
‫مصيطر على موارد الحاسوب والدوال التي تساعد في عملية التبديل هي‪-:‬‬
‫‪ Join‬و ‪ Sleep‬وغيرها من الدوال‪.‬‬

‫?‪34\ What join() method does‬‬

‫السؤال‪ :‬الدالة ‪ Join‬ما هو عملها؟‬


‫الجواب‪ :‬تعمل الدالة ‪ Join‬على إيقاف تنفيذ جميع الثريدات حتى يكمل الثريد الذي استدعاها‬
‫تنفيذه ثم تعود الثريدات األخرى إلكمال تنفيذها‪.‬‬

‫?‪35\ What sleep(time) method does‬‬

‫السؤال‪ :‬الدالة ‪ Sleep‬ما هو عملها؟‬


‫الجواب‪ :‬تعمل الدالة ‪ Sleep‬على إيقاف تنفيذ الثريد المستدعي لها لفترة محددة من الزمن تقدر‬
‫بالملي ثانية‪.‬‬

‫?‪36\ What start () method does‬‬


‫السؤال‪ :‬الدالة ‪ Start‬ما هو عملها؟‬
‫الجواب‪ :‬هي الدالة التي تسمح للثريد ببدء عمله إي أن الثريد ال يمكنه بدء التنفيذ من دون هذه‬
‫الدالة‪.‬‬
‫?‪37\ What abort() method does‬‬

‫السؤال‪ :‬الدالة ‪ abort‬ما هو عملها؟‬


‫الجواب‪ :‬الدالة ‪ abort‬تعمل على إحباط (إيقاف)عمل الثريد الذي استدعاها حاال وال تسمح له‬
‫بإكمال تنفيذه‪.‬‬
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
100 ‫ الى‬1 ‫الحظ كيف تم إحباط عمل الثريد قبل أن يطبع من‬
using System;
using System.Threading;
namespace xxyy
{
class Program
{
public static void fun1()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(i);
Thread.Sleep(200);
}
}
static void Main(string[] args)
{
Console.WriteLine("Main Thread Started");
Thread t1 = new Thread(fun1);
t1.Start();
t1.Abort();
Console.WriteLine("Main Thread finish");
Console.ReadKey();
} } }

38\ How can you pause the execution of a Thread for a


certain amount of time?
‫ كيف يمكن أن نوقف تنفيذ الثريد لفترة محددة من الزمن؟‬:‫السؤال‬
.Sleep ‫ يكننا أن نوقف عمل الثريد لفترة محدد من الزمن باستخدام الدالة‬:‫الجواب‬
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
-:‫المثال التالي يوضح كيفية إيقاف الثريد لفترة محددة‬
using System;
using System.Threading;
namespace xxyy
{
class Program
{
public static void fun1()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(Thread.CurrentThread.Name+"is started");
Console.WriteLine(Thread.CurrentThread.Name + " Sleep for
Second " + "\n");
Thread.Sleep(1000);
}
}
static void Main(string[] args)
{
Console.WriteLine("Main Thread Started");
Thread t1 = new Thread(fun1);
t1.Name = "Thread 1";
t1.Start();
Console.WriteLine("Main Thread finish");
Console.ReadKey();
}
} }

39\ What are the classes used in System.Threading


Namespace ?

‫ ؟‬System.Threading ‫ ماهي الفصائل المستخدمة في فضاء األسماء‬:‫السؤال‬


. ThreadPool ‫ والفصيل‬Thread ‫ الفصائل المستخدمة هي الفصيل‬:‫الجواب‬
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
:‫الجدول التالي يوضح الفرق بينهما‬
ThreadPool Thread
‫ لتنفيذ الثريد‬Start ‫ال يحتاج للدالة‬ ‫ لتنفيذ الثريد‬Start ‫يحتاج للدالة‬
‫ بشكل‬Background ‫يعتبر‬ ‫ بشكل‬Foreground ‫يعتبر‬
‫افتراضي‬ ‫افتراضي‬
.Mutex ‫ والفصيل‬Monitor ‫وأيضا الفصائل التي توفر دوال لإلقفال وهي الفصيل‬

40\ How to pass the parameter in Thread?


‫ كيف نمرر البارامترات للثريد؟‬:‫السؤال‬
..... .‫ نمرر البارامترات للثريد باستخدام التعبيرات المدا وهناك عدة طرق أخرى‬:‫الجواب‬
‫المثال التالي يوضح طريقة استخدام التعبير المدا‬
Thread t1 = new Thread(()=>fun1(4,7));
using System;
using System.Threading;
namespace xxyy
{
class Program
{
public static void fun1(int x, int y)
{
Console.WriteLine("the summatiom of "+ x +" + " + y+" =
"+Convert.ToInt16( x+y));
}
static void Main(string[] args)
{
Console.WriteLine("Main Thread Started");
Thread t1 = new Thread(()=>fun1(4,7));
t1.Start();
Console.WriteLine("Main Thread finish");
Console.ReadKey();
}
} }
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
41\ Why we use lamda?

‫؟‬lambda ‫ لماذا نستخدم التعبير‬:‫السؤال‬


‫ نستخدم التعبير المدا حتى نتمكن من تمرير دالة تحتوي على بارامترات للثريد‬:‫الجواب‬
.‫الن الثريد ال يقبل اال الدوال التي ال تحتوي على بارامترات‬

Write The Following programs :

- :‫اكتب البرامج التالية‬


1 \ W rite a program include f our threads to show the
problem Of the deadlock using monitor and mutex?

‫اكتب برنامج يحتوي على أربعة ثريدات لعرض مشكلة اإلقفال الميت‬
‫ ؟‬mutex ‫ و‬monitor ‫باستخدام‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
Monitor.Enter(ob1);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
Monitor.Enter(ob2);
Console.WriteLine(t.Name + " got lock ob2 ");
mutex.ReleaseMutex();
Monitor.Exit(ob1);
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
Monitor.Enter(ob1);
Console.WriteLine(t.Name+" got lock again ob1 ");
// Monitor.Exit(ob1);
mutex.ReleaseMutex();
Monitor.Exit(ob2);
}
public static void fun3()
{
Monitor.Enter(ob3);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob3 ");
Thread.Sleep(500);
Monitor.Enter(ob4);
Console.WriteLine(t.Name + " got lock ob4 ");
// Monitor.Exit(ob4);
mutex.ReleaseMutex();
Monitor.Exit(ob3);
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
public static void fun4()
{
Monitor.Enter(ob4);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob4 ");
Thread.Sleep(500);
Monitor.Enter(ob3);
Console.WriteLine(t.Name + " got lock ob3 ");
mutex.ReleaseMutex();
// Monitor.Exit(ob4);
mutex.ReleaseMutex();
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
Thread t3 = new Thread(fun3);
Thread t4 = new Thread(fun4);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t3.Name = "Thread 3";
t4.Name = "Thread 4";
t1.Start(); t2.Start();
t3.Start(); t4.Start();

// Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬

2\ Write a program include f our threads to show the


problem Of the deadlo ck using monitor and keyword
lock?
‫اكتب برنامج يحتوي على أربعة ثريدات لعرض مشكلة اإلقفال الميت‬
‫؟‬lock () ‫ و‬monitor ‫باستخدام‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();

public static void fun1()


{
Monitor.Enter(ob1);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
lock (ob2)
{
Console.WriteLine(t.Name + " got lock ob2 ");
}
Monitor.Exit(ob1);
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
public static void fun2()
{
lock (ob2)
{
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
Monitor.Enter(ob1);
Console.WriteLine(t.Name + " got lock agine ob1 ");
Monitor.Exit(ob1);
}
}
public static void fun3()
{
Monitor.Enter(ob3);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob3 ");
Thread.Sleep(500);
Monitor.Enter(ob4);
Console.WriteLine(t.Name + " got lock ob4 ");
Monitor.Exit(ob4);
Monitor.Exit(ob3);
}
public static void fun4()
{
Monitor.Enter(ob4);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob4 ");
Thread.Sleep(500);
lock(ob3)
{
Console.WriteLine(t.Name + " got lock ob3 ");
}
Monitor.Exit(ob4);
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
Thread t3 = new Thread(fun3);
Thread t4 = new Thread(fun4);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t3.Name = "Thread 3";
t4.Name = "Thread 4";
t1.Start(); t2.Start();
t3.Start(); t4.Start();

// Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
} }
-:‫ فقط‬Monitor ‫المثال التالي باستخدام ال‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();
public static void fun1()
{
Monitor.Enter(ob1);
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name +" got lock ob1 ");
Thread.Sleep(500);
Monitor.Enter(ob2);
Console.WriteLine(t.Name + " got lock ob2 ");
Monitor.Exit(ob2);
Monitor.Exit(ob1);
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
Monitor.Enter(ob1);
Console.WriteLine(t.Name + " got lock ob1 ");
Monitor.Exit(ob1);
Monitor.Exit(ob2);
}
public static void fun3()
{
Monitor.Enter(ob3);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob3 ");
Thread.Sleep(500);
Monitor.Enter(ob4);
Console.WriteLine(t.Name + " got lock ob4 ");
Monitor.Exit(ob4);
Monitor.Exit(ob3);
}
public static void fun4()
{
Monitor.Enter(ob4);
Thread t = Thread.CurrentThread;
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Console.WriteLine(t.Name + " got lock ob4 ");
Thread.Sleep(500);
Monitor.Enter(ob3);
Console.WriteLine(t.Name + " got lock ob3 ");
Monitor.Exit(ob3);
Monitor.Exit(ob4);
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
Thread t3 = new Thread(fun3);
Thread t4 = new Thread(fun4);
t1.Name = "Thread 1"; t2.Name = "Thread 2";
t3.Name = "Thread 3"; t4.Name = "Thread 4";
t1.Start();
t2.Start();
t3.Start();
t4.Start();
// Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

3\ Write a program include four threads to show the


problem Of the deadlock using monitor , lock(..) and
mutex?
‫اكتب برنامج يحتوي على أربعة ثريدات لعرض مشكلة اإلقفال الميت باستخدام‬
‫؟‬Mutex‫ و‬lock)( ‫ و‬Monitor
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static object ob3 = new object();
public static object ob4 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
Monitor.Enter(ob1);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
lock (ob2)
{
Console.WriteLine(t.Name + " got lock ob2 ");
}
mutex.ReleaseMutex();
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
Monitor.Enter(ob1);
Console.WriteLine(t.Name + " got lock agine ob1 ");
// Monitor.Exit(ob1);
mutex.ReleaseMutex();
Monitor.Exit(ob2);
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
public static void fun3()
{
lock (ob3)
{
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob3 ");
Thread.Sleep(500);
Monitor.Enter(ob4);
Console.WriteLine(t.Name + " got lock ob4 ");
// Monitor.Exit(ob4);
mutex.ReleaseMutex();
Monitor.Exit(ob3);
}
}
public static void fun4()
{
Monitor.Enter(ob4);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob4 ");
Thread.Sleep(500);
Monitor.Enter(ob3);
Console.WriteLine(t.Name + " got lock ob3 ");
mutex.ReleaseMutex(); //
Monitor.Exit(ob4);
mutex.ReleaseMutex();
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
Thread t3 = new Thread(fun3);
Thread t4 = new Thread(fun4);
t1.Name = "Thread 1";
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
t2.Name = "Thread 2";
t3.Name = "Thread 3";
t4.Name = "Thread 4";

t1.Start(); t2.Start();
t3.Start(); t4.Start(); //
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

4\ Write a program include two threads to show the


problem Of the deadlock using monitor?

‫اكتب برنامج يحتوي على ثريدين ليعرض مشكلة اإلقفال الميت باستخدام‬
‫ ؟‬monitor ‫ال‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
// mutex.WaitOne();
Monitor.Enter(ob1);
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
// mutex.WaitOne();
Monitor.Enter(ob2);
// mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock agine ob2 ");
// mutex.ReleaseMutex();
Monitor.Exit(ob2);
Monitor.Exit(ob1);
}
public static void fun2()
{
// mutex.WaitOne();
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob1 ");
Thread.Sleep(500);
// mutex.WaitOne();
Monitor.Enter(ob1);
// mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock agine ob2 ");
// mutex.ReleaseMutex();
Monitor.Exit(ob1);
Monitor.Exit(ob2);
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
t1.Name = "Thread 1"; t2.Name = "Thread 2";
t1.Start(); t2.Start();
// Console.WriteLine("Main Thread finshed");
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Console.ReadKey();
}
} }
5\ Write a program include two threads to show the
problem Of the deadlock using mutex?
‫ اكتب برنامج يحتوي على ثريدين ليعرض مشكلة اإلقفال الميت باستخدام‬:‫السؤال‬
‫؟‬mutex ‫ ال‬-: Momitor ‫ و‬Mutex ‫باستخدام ال‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static Mutex mutex = new Mutex();
public static void fun1()
{
Monitor.Enter(ob1);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name +" got lock ob1 ");
Thread.Sleep(500);
Monitor.Enter(ob2);
mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock ob2 ");
mutex.ReleaseMutex();
}
public static void fun2()
{
Monitor.Enter(ob2);
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Thread.Sleep(500);
Monitor.Enter(ob1);
mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock ob1 ");
mutex.ReleaseMutex();
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start();
t2.Start();
t2.Join();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

lock ‫باستخدام‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static object ob1 = new object();
public static object ob2 = new object();
public static Mutex mutex = new Mutex();
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
public static void fun1()
{
// mutex.WaitOne();
lock (ob1)
{
{
Thread t = Thread.CurrentThread;

Console.WriteLine(t.Name + " got lock ob1 ");


Thread.Sleep(500);
// mutex.WaitOne();
lock (ob2)
{
// mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock ob2 ");
// mutex.ReleaseMutex();
}
}
}
}

public static void fun2()


{
// mutex.WaitOne();
lock (ob2)
{
{
Thread t = Thread.CurrentThread;
Console.WriteLine(t.Name + " got lock ob2 ");
Thread.Sleep(500);
// mutex.WaitOne();
lock (ob1)
{
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
// mutex.ReleaseMutex();
Console.WriteLine(t.Name + " got lock ob1 ");
// mutex.ReleaseMutex();
}
}
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun2);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start(); t2.Start();

// Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}

6\ Write a program include two Foreground threads?

‫ ؟‬Foreground ‫اكتب برنامج يحتوي على ثريدين من نوع‬

using System;
using System.Threading;
namespace FirstThread
{
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);

T1.Name = "Thread 1";


T2.Name = "Thread 2";
T1.IsBackground = false; //foreground Thread
T2.IsBackground = false;
//foreground Thread
T1.Start();
T2.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬

7\ Write a program include two Background threads?

‫؟‬Background ‫اكتب برنامج يحتوي على ثريدين من نوع‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);

T1.Name = "Thread 1";


T2.Name = "Thread 2";
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬

T1.IsBackground = true;
T2.IsBackground = true;
T1.Start();
T2.Start();

Console.WriteLine("Main Thread finished");


Console.ReadKey();
}
}
}

8\ Write a program shows the difference between


Foreground and Background threads?

‫ و الثريد‬Foreground ‫اكتب برنامج يعرض االختالف بين الثريد من نوع‬


‫ ؟‬Background ‫من نوع‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);

T1.Name = "Thread 1";


T2.Name = "Thread 2";

T1.IsBackground = true;
//Background Thread
T2.IsBackground = false;
//foreground Thread
T1.Start();
T2.Start();

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}

9\ Write a program include two Background threads?

‫ ؟‬Background ‫اكتب برنامج يحتوي على ثريدين من نوع‬


using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);

T1.Name = "Thread 1";


T2.Name = "Thread 2";

T1.IsBackground = true;
T2.IsBackground = true;
T1.Start();
T2.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

10\Use and show the time Slicing in program includes


two threads?
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
‫اكتب برنامج يعرض عملية تقطيع الوقت يحتوي على ثريدين؟‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Console.WriteLine(t.Name+" Sleep for one Second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);
T1.Name = "Thread 1";
T2.Name = "Thread 2";
T1.Start();
T2.Start();
T1.Join();

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
}

11\ Write a program includes three threads to show


context switching?
‫اكتب برنامج يحتوي على ثالثة ثريدات ليعرض عملية تبديل السياق)تبديل‬
‫التنفيذ من ثريد ألخر(؟‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}

static void Main(string[] args)


{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Thread T2 = new Thread(show);
Thread T3 = new Thread(show);

T1.Name = "Thread 1";


773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
T2.Name = "Thread 2";
T3.Name = "Thread 3";
T1.Start();
T2.Start();
T3.Start();
T1.Join();

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}

12\ Write a program to show life cycle of thread?

‫اكتب برنامج يعرض دورة حياة الثريد؟‬


‫ ألنها الخاصية المسؤولة عن تحديد حالة‬ThreadState ‫نستخدم الخاصية‬
.‫الثريد‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
Console.WriteLine(" the priority of " + t.Name + " is "
+ t.Priority+"\n");
for (int i = 0; i <5; i++)
{
Console.WriteLine(t.Name + " Started");
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Thread.Sleep(1000);
Console.WriteLine("Life Thread is " + t.ThreadState);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
Console.WriteLine("Life Thread is "+ T1.ThreadState);
T1.Name = "Thread 1";
T1.Start();
Console.WriteLine("Life Thread is " + T1.ThreadState);
//T1.Join();
T1.Abort();
Console.WriteLine("Life Thread is " + T1.ThreadState);

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}

13\ Write a program shows safety threads?

‫اكتب برنامج يعرض الثريدات االمنة؟‬

‫ هي الثريدات التي ال تحصل بينها عملية تداخل اثناء‬- :‫الثريدات األمنة‬


.‫التنفيذ و لمنع عملية التداخل نستخدم أحد أنواع االقفال‬

using System;
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
using System.Threading;
namespace FirstThread
{
class Program
{
static object ob = new object();
public static void lock1()
{
lock (ob)
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(t.Name + " Started running");
Console.WriteLine(t.Name + " wait for second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(lock1);
Thread t2 = new Thread(lock1);
Thread t3 = new Thread(lock1);

t1.Name = "Thread 1";


t2.Name = "Thread 2";
t1.Start();
t2.Start();
t3.Name = "Thread 3";
t3.Start();
t3.Join();
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

14\ Write a program shows non safety threads?

‫اكتب برنامج يعرض الثريدات غير االمنة؟‬


‫ هي الثريدات التي تحصل بينها عملية تداخل اثناء‬- :‫الثريدات غير االمنة‬
.‫التنفيذ‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static object ob = new object();
public static void fun1()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(t.Name + " Started running");
Console.WriteLine(t.Name + " wait for second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Thread t1 = new Thread(fun1);
Thread t2 = new Thread(fun1);
Thread t3 = new Thread(fun1);

t1.Name = "Thread 1";


t2.Name = "Thread 2";
t1.Start();
t2.Start();
t3.Name = "Thread 3";
t3.Start();
t3.Join();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

15\ Write a program to show the Synchronization?

‫اكتب برنامج ليعرض عملية التزامن؟‬


‫ او عن طريق‬Sleep)( ‫ والدالة‬join)( ‫نحقق عملية التزامن عن طريق استخدام الدالة‬
Sleep)( ‫ والدالة‬join)( ‫ بنحل باستخدام الدالة‬.‫استخدام احد أنواع اإلقفال‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
public static void syn ()
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
Console.WriteLine(t.Name + " Started running");
Console.WriteLine(t.Name + " wait for second");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(syn);
Thread t2 = new Thread(syn);
Thread t3 = new Thread(syn);

t1.Name = "Thread 1";


t2.Name = "Thread 2";
t1.Start(); t1.join();
t2.Start(); t2.join();
t3.Name = "Thread 3";
t3.Start(); t3.join();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
} }
16\ Write a program to show the Synchronization using

mutex?
‫ ؟‬mutex ‫اكتب برنامج ليعرض عملية المزامنة باستخدام الكلمة‬
using System;
using System.Threading;
namespace FirstThread
{
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
class Program
{
static object ob1 = new object();
static Mutex mutex = new Mutex();

public static void locked ()


{
mutex.WaitOne();
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(t.Name + " Started lock");
Console.WriteLine(t.Name + " lock ob1");
Thread.Sleep(1000);
}
Console.WriteLine(t.Name + " unlock ob1");
Console.WriteLine("\n");
mutex.ReleaseMutex();
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(locked);
Thread t2 = new Thread(locked);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start(); t2.Start();
t2.Join();

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
} }

17\ Write a program to show the Synchronization using


monitor?

‫ ؟‬monitor ‫اكتب برنامج ليعرض عملية المزامنة باستخدام الكلمة‬

using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static object ob1 = new object();
public static void locked ()
{
Monitor.Enter(ob1);
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(t.Name + " Started lock");
Console.WriteLine(t.Name + " lock ob1");
Thread.Sleep(1000);
}
Console.WriteLine(t.Name + " unlock ob1");
Console.WriteLine("\n");
Monitor.Exit(ob1);
}
}
static void Main(string[] args)
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(locked);
Thread t2 = new Thread(locked);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start(); t2.Start();
t2.Join();

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
} }

18\ Write a program to show the Synchronization using


Keyword lock?

‫ ؟‬lock ‫اكتب برنامج ليعرض عملية المزامنة باستخدام الكلمة‬


using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static object ob1 = new object();
public static void locked ()
{
lock (ob1)
{
Thread t = Thread.CurrentThread;
for (int i = 0; i < 5; i++)
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
Console.WriteLine(t.Name + " Started lock");
Console.WriteLine(t.Name + " lock ob1");
Thread.Sleep(1000);
}
Console.WriteLine(t.Name + " unlock ob1");
Console.WriteLine("\n");
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread t1 = new Thread(locked);
Thread t2 = new Thread(locked);
t1.Name = "Thread 1";
t2.Name = "Thread 2";
t1.Start(); t2.Start();

Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}

19\ Write a program includes 3 threads to show the


interference?

‫اكتب برنامج يحتوي على ثالثة ثريدات ليعرض عملية التداخل؟‬


using System;
using System.Threading;
namespace FirstThread
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
class Program
{
static void show ()
{
Thread t = Thread.CurrentThread;
Console.WriteLine(" the priority of " + t.Name + " is "
+ t.Priority+"\n");
for (int i = 0; i <10; i++)
{
Console.WriteLine(t.Name + " Started");
Thread.Sleep(1000);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(show);
T1.Name = "Thread 1";
Thread T2 = new Thread(show);
T2.Name = "Thread 2";
Thread T3 = new Thread(show);
T3.Name = "Thread 3";
T1.Start();
T2.Start();
T3.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
20\ Use Lamda expression in program includes two
threads and what is the benefit of usage Lamda
expression?
‫استخدم التعبير المدا في برنامج يحتوي على ثريدين و ماهي فاية التعبير‬
‫المدا؟‬

.‫الفائدة من التعبير المدا سبق شرحها‬


using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void print (int x , int y)
{
Thread t = Thread.CurrentThread;
Console.WriteLine(" the priority of " + t.Name + " is "
+ t.Priority+"\n");

Console.WriteLine(t.Name+" Started ");


Console.WriteLine("the summation of "+ x +" + "+ y+ " =
"+Convert.ToInt32( x+y).ToString() );
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(()=>print(10,9));
T1.Name = "child 1";
Thread T2 = new Thread(()=>print(3,5));
T2.Name = "chil 2";
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
T1.Priority = ThreadPriority.Highest;
T1.Start();
T2.Start();

//Console.WriteLine("Main Thread finshed");


Console.ReadKey();
}
}
}

21\write program shows scheduling of Thread?

‫اكتب برنامج يعرض جدولة الثريد؟‬


using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void print ()
{
Thread t = Thread.CurrentThread;
Console.WriteLine(" the priority of " + t.Name + " " +
t.Priority+"\n");

Console.WriteLine("child Thread Started " +t.Name);

for(int i = 1; i <= 3; i++)


{
Console.WriteLine(" the child Thread Running " +t.Name);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(print);
T1.Name = "child 1";
Thread T2 = new Thread(print);
T2.Name = "chil 2";
Thread T3 = new Thread(print);
T3.Name = "chil 3";
T1.Priority = ThreadPriority.Highest;
T2.Priority = ThreadPriority.Lowest;
T3.Priority = ThreadPriority.BelowNormal;
T1.Start();
T2.Start();
T3.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

22\write program shows using priorities of Thread?

‫اكتب برنامج يعرض أولويات الثريد؟‬


using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void print ()
{
Thread t = Thread.CurrentThread;
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Console.WriteLine(" the priority of " + t.Name + " " +
t.Priority+"\n");

Console.WriteLine("child Thread Started " +t.Name);


for(int i = 1; i <= 3; i++)
{
Console.WriteLine(" the child Thread Running " +t.Name);
}
Console.WriteLine("\n");
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started\n");
Thread T1 = new Thread(print);
T1.Name = "child 1";
Thread T2 = new Thread(print);
T2.Name = "chil 2";
Thread T3 = new Thread(print);
T3.Name = "chil 3";
T1.Priority = ThreadPriority.Highest;
T2.Priority = ThreadPriority.Lowest;
T3.Priority = ThreadPriority.BelowNormal;
T1.Start();
T2.Start();
T3.Start();
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
23\ Write a program includes two Threads to prevent the
interference?

‫اكتب برنامج يحتوي على ثريدين لتمنع عملية التداخل؟‬


‫ او عن طريق استخدام احد‬join)( ‫نمنع عملية التداخل عن طريق استخدام الدالة‬
: join)( ‫ بنحل باستخدام الدالة‬.‫أنواع اإلقفال‬
using System;
using System.Threading;
namespace FirstThread
{
class Program
{
static void print ()
{
Thread t = Thread.CurrentThread;
Console.WriteLine("child Thread Started " +t.Name);
for(int i = 1; i <= 5; i++)
{
Console.WriteLine("the child Thread Running " +t.Name );
}
}

static void Main(string[] args)


{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started");
Thread T1 = new Thread(print);
T1.Name = "child1";
Thread T2 = new Thread(print);
T2.Name = "chil2";
Console.WriteLine(T1.Name + " ChildThread state is :" +
T1.ThreadState);
T1.Start();
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
Console.WriteLine(T1.Name+" ChildThread state is :" +
T1.ThreadState);
Console.WriteLine(T2.Name + " ChildThread state is :" +
T2.ThreadState);
T1.Join();
T2.Start();
T2.Join();
Console.WriteLine(" Main Thread state is :" +
th.ThreadState);
Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

24\Write a Program shows using Property ThreadState


after and before the methods used in thread class?

‫ قبل وبعد الدوال‬ThreadState ‫اكتب برنامج يوضح استخدام الخاصية‬


‫ ؟‬Thread ‫المستخدمة في الفصيل‬
‫ والسطر التالي يوضح‬.‫ عملها هو تحديد حاله الثريد‬ThreadState ‫الخاصية‬
‫طريقة استخدامها‬
Console.WriteLine("ChildThread state is:"+ T.ThreadState);

using System;
using System.Threading;
namespace secondThread
{
class Program
{
static void print ()
773133872  ‫ أكـــرم عبدالكريم لقــمان‬/‫ أ‬:‫تم مراجعته من قبل‬
{
Console.WriteLine("child Thread Started");
for(int i = 1; i <= 5; i++)
{
Console.WriteLine(" the child Thread Running");
}
}
static void Main(string[] args)
{
Thread th = Thread.CurrentThread;
Console.WriteLine("Main Thread Started");
Thread T = new Thread(print);
Console.WriteLine("ChildThread state is :" +T.ThreadState);
T.Start();
Console.WriteLine("ChildThread state is :" +T.ThreadState);
T.Join();
Console.WriteLine("ChildThread state is :" +T.ThreadState);

Console.WriteLine("Main Thread state is :"+th.ThreadState);


Console.WriteLine("Main Thread finshed");
Console.ReadKey();
}
}
}

You might also like