Threads
A thread is defined as a path of execution of a program. Each thread has a unique flow of control.Threads are most useful in applications where complicated and time consuming operations are performed. These operations can be split up into smaller chunks and each chunk is operated by a single thread. So multiple chunks can be operated upon by a series of threads.
One common example of use of thread is implementation of concurrent programming by modern operating systems. Good usage of threads can lead to better utilization of CPU cycles as well as creation of highly efficient applications.
If you have been following the blog, we have been working with programs that only execute on a single thread ( main thread ). However, this way the application can perform only one job at a time. To make it execute more than one task at a time, it could be divided into smaller tasks and each of those tasks given to different threads.
Thread Life Cycle
- Unstarted State : Instance of thread created but 'Start' function not yet called.
- Ready State : Ready to run but it's waiting for CPU cycle.
- Not Runnable State : Not currently possible to run the thread because of I/O blocking or if it's sleeping.
- Dead State : Execution has completed or the thread has been aborted.
Basics Of Using Threads In C#
We need to use System.Threading in order to access threads.
We create a thread like this : Thread thread = new Thread( method to invoke )
The method will be executed parallel after we start the thread.We start the execution of the thread like this : thread.Start().
Now the thread has started executing the method that was passed to it.
But the thing with threads is that you can not predetermine how long a thread will take to finish the task, so in order to make sure our program does not end before the work of the thread is done, we have to make sure it waits for the thread work to be done.
To do that we tell the running thread to join the execution path of the main thread.
So we write this : thread.Join().
If we want the execution of the thread to stop mid-way, then we can call abort, which will stop thread execution permanently. This is done like this : thread.Abort().
*just for now, Thread.Sleep(int milliseconds) :- Makes the thread sleep for that many milliseconds ( acts as a placeholder for a complex and long running task)
Example Program
using System;
using System.Threading;
namespace BitShiftProgrammer
{
public class Program
{
static void PrintHelloA()
{
Thread.Sleep(1000);
Console.WriteLine("Printed Hello A");
}
static void PrintHelloB()
{
Thread.Sleep(500);
Console.WriteLine("Printed Hello B");
}
public static void Main(string[] args)
{
Thread th1 = new Thread(PrintHelloA);
Thread th2 = new Thread(PrintHelloB);
th1.Start();
th2.Start();
th2.Join();
th1.Join();
}
}
}
Printed Hello B Printed Hello AAs you can see execution of th2 ended earlier than th1 even though th1 was called earlier. This can often be the case with threads. There is not predictable order of execution end, so we often have mechanisms in place which allow us to deal with these kinds of situations. We will go over them later.
More thread related tutorials will show up in the future.
For more C# tutorials head on over HERE.
For Unity tutorials check THIS.