Threads
Threads give the appearance
that an application is performing multiple tasks in parallel. Threads live in
one process and take individual paths of execution. Threads have different priorities,
such as REAL-TIME, HIGH, NORMAL, and IDLE.. The CPU will run the threads with
higher priorities first causing one thread to never get a chance to run; this
is called starvation. There are three thread states – running, ready, and
blocked. Running means that the thread is already running; ready means that the
thread is out of the blocked state and is ready for the available resources so
that it can move to the running state; and blocked means that the thread cannot
run. Blocked threads can never go straight to running; they must be ready
first. To better understand threads, here’s an example. Take in two numbers
from the user. The first number will be the number of threads to create and
run, and the second number will be the amount of time, in milliseconds, for
each thread to sleep. When each one of those threads is created, have it print
that it is alive and then tell it to sleep.
Thread Creation:
To create a thread, you first need to tell the compiler that you will be using System.Threading. Create a variable of type Thread and tell it to get a new Thread, and tell it what procedure for which the thread will be responsible. After you finish creating the thread, tell it to start running by using the Start() command.
Your code to solve the problem stated in the intro should look similar to the code below (the highlighted code is the code needed to create a thread).
|
using System; using
System.Threading; namespace Threads { /// <summary> /// Summary description
for Class1. /// </summary> class ThreadMe { private static int
threadNumber = 1; private static int
sleepLeng = 0; private static Thread [] myThreads; /// <summary> /// The main entry point
for the application. /// </summary> [STAThread] static void { Console.Write("Please
enter the number of threads you wish to generate: "); int numberOfThreads =
Int32.Parse(Console.ReadLine()); myThreads
= new Thread[numberOfThreads]; Console.Write("How long do you want the threads to
sleep?: "); sleepLeng
= Int32.Parse(Console.ReadLine()); StartThreads(numberOfThreads); } static void StartThreads (int
numThreads) { for (int i = 0; i
< numThreads; i++) { Thread theThread = new
Thread (new ThreadStart (StartFunction)); theThread.Start(); myThreads[i]
= theThread; } } static void StartFunction () { Console.WriteLine
("Thread " + threadNumber + " is
alive."); int threadNum = threadNumber; threadNumber++; Console.WriteLine
("Thread " + threadNum + " is
sleeping."); Thread.Sleep(sleepLeng); Console.WriteLine
("Thread " + threadNum + " is
awake."); } } } |
As the problem in the lab
intro stated, the above application needs to prompt the user for the number of
threads to create and the amount of time the threads should sleep. This
is accomplished through the use of the Console.Write and Console.ReadLine commands (remember that each time you try to read in from
the Console, it returns a String. In this case, you want an int. You will need
to use the Int32.Parse command to convert the String
to an int. Now that we have the
needed information from the user, it is time to move on to the creation of our
threads. Again, this means we will need to tell the compiler we are
using System.Threading. Create the
thread and tell it the procedure it is responsible for executing, and start the
thread. To create and start our threads we will need to use the code
found below:
|
Thread theThread
= new Thread (new
ThreadStart (StartFunction)); theThread.Start(); |
As you can see in our application code, the above code segment is found within a for loop. Remember, rather than creating just one thread, we will be creating multiple threads. Thus, by putting the above code in a loop, we are able to spawn off as many threads as needed and place them in our array of threads. Also in our application code, the size of the array is determined at run time by the number of threads the user wants to create. Also the procedure that each thread will be responsible for executing is the StartFunction procedure. This procedure will print out the necessary information to the user and will also tell the thread to sleep for the established length of time (processing cycles).