Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

In Fun2()

Thread Name: Main Thread, ThreadState: Background, Stopped, WaitSleepJoin Thread Name: First Thread, ThreadState: Stopped

Thread Name: Second Thread, ThreadState: Running

Press any key to continue

The important thing to note here is the sequence of execution and the thread states at different points during the execution of the program. The two threads (firstThread and secondThread) didn't get started even when the Main() method was exiting. At the end of firstThread, the Main() thread has stopped while the secondThread is in the Sleep state.

Thread Demonstration Example - Thread Priority

When two or more threads are executing simultaneously they share the processor time. In normal conditions, the operating system tries to distribute the processor time equally amongst the threads of a process. However, if we wish to influence how processor time is distributed, we can also specify the priority level for our threads. In .Net we do this using the System.Threading.ThreadPriority enumeration, which contains Normal, AboveNormal, BelowNormal, Highest and Lowest. The default priority level of a thread is, to no one's surprise, Normal. A thread with a higher priority is given more time by Operating System than a thread with a lower priority. Consider the program below with no priority setting:

class Test

{

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1));

Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Name = "First Thread"; secondThread.Name = "Second Thread";

firstThread.Start();

secondThread.Start();

}

public static void Fun1()

{

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

{

int t = new Random().Next(20); for(int j=0; j<t; j++)

274

Programmers Heaven: C# School

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + " empty strings");

}

}

public static void Fun2()

{

for(int i=20; i>=11; i--)

{

int t = new Random().Next(20); for(int j=0; j<t; j++)

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + "empty strings");

}

}

}

Here we have asked the runtime to create an almost similar number of objects in the two thread methods (Fun1() and Fun2()). One possible output of the program is:

First Thread: created: 18 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

First Thread: created: 5 empty strings

Second Thread: created: 16 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

275

Programmers Heaven: C# School

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Second Thread: created: 5 empty strings

Press any key to continue

Now we will change the priority of secondThread to AboveNormal:

class Test

{

static void Main()

{

Thread firstThread = new Thread(new ThreadStart(Fun1)); Thread secondThread = new Thread(new ThreadStart(Fun2));

firstThread.Name = "First Thread"; secondThread.Name = "Second Thread";

secondThread.Priority = ThreadPriority.AboveNormal;

firstThread.Start();

secondThread.Start();

}

public static void Fun1()

{

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

{

int t = new Random().Next(20); for(int j=0; j<t; j++)

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + " empty strings");

}

}

public static void Fun2()

{

for(int i=20; i>=11; i--)

{

276

Programmers Heaven: C# School

int t = new Random().Next(40); for(int j=0; j<t; j++)

{

new String(new char[] {});

}

Console.WriteLine(Thread.CurrentThread.Name +

": created: " + t.ToString() + " empty strings");

}

}

}

Here we have made two changes. We have increased the priority of the secondThread to AboveNormal. We have also increased the range for random numbers so that the second thread would be required to create a greater number of objects. On compiling and executing the program, we get output like:

Second Thread: created: 14 empty strings

Second Thread: created: 18 empty strings

Second Thread: created: 18 empty strings

Second Thread: created: 18 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

Second Thread: created: 27 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

First Thread: created: 13 empty strings

Press any key to continue

Consider the above output. Although the second thread is creating more objects, it still finishes before the first thread. The reason simply is that now the priority level of second thread is higher than that of the first thread.

277

Соседние файлы в папке books