Multithreading in C#
What is Multithreading in C#.
Multithreading gives programs the ability to do several things at a time. Each stream of execution is called a thread. Multithreading is used to divide lengthy tasks into different segments that would Secondary threadwise abort programs. Threads are mainly used to utilize the processor to a maximum extent by avoiding it's idle time. Threading lets a program seem as if it is executing several tasks at once. What actually happens is, the time gets divided by the computer into parts and when a new thread starts, that thread gets a portion of the divided time. Threads in VB .NET are based on the namespace System.Threading. Represents a thread that executes with in theCLR. Using this type, one can able to spawn additional threads in theowning AppDomain.This type defines a number of methods (both static andshared) that allow us to create new threads from a current thread, aswell as suspend, stop, and destroy a given thread.
The following example will demonstrate threads using C#.
Multithreading gives programs the ability to do several things at a time. Each stream of execution is called a thread. Multithreading is used to divide lengthy tasks into different segments that would Secondary threadwise abort programs. Threads are mainly used to utilize the processor to a maximum extent by avoiding it's idle time. Threading lets a program seem as if it is executing several tasks at once. What actually happens is, the time gets divided by the computer into parts and when a new thread starts, that thread gets a portion of the divided time. Threads in VB .NET are based on the namespace System.Threading. Represents a thread that executes with in theCLR. Using this type, one can able to spawn additional threads in theowning AppDomain.This type defines a number of methods (both static andshared) that allow us to create new threads from a current thread, aswell as suspend, stop, and destroy a given thread.
The following example will demonstrate threads using C#.
using System;
using System.Threading;
public class Test
{
static void Main()
{
ThreadStart NewThred = new ThreadStart(NewThread);
Thread thread = new Thread(NewThred);
thread.Start();
for (int i=0; i < 5; i++)
{
Console.WriteLine ("Primary thread: {0}", i);
Thread.Sleep(1000);
}
}
static void NewThread ()
{
for (int i=0; i < 10; i++)
{
Console.WriteLine ("Secondary thread: {0}", i);
Thread.Sleep(500);
}
}
}
Here we creates a new thread which runs the NewThread method, and starts it. That thread counts from 0 to 9 fairly fast while the Primary thread counts from 0 to 4 fairly slowly The way they count at different speeds is by each of them including a call to
Thread.Sleep
, which just makes the current thread sleep for the specified period of time. Between each count in the Primary thread we sleep for 1000ms, and between each count in the Secondary thread we sleep for 500ms. Here are the results from one test run on my machine: Primary thread: 0 Secondary thread: 0 Secondary thread: 1 Primary thread: 1 Secondary thread: 2 Secondary thread: 3 Primary thread: 2 Secondary thread: 4 Secondary thread: 5 Primary thread: 3 Secondary thread: 6 Secondary thread: 7 Primary thread: 4 Secondary thread: 8 Secondary thread: 9 |
excellent
ReplyDelete