When one programs uses multiple threads to accomplish its tasks, it is called multithreading.
Multithreading doesn’t guaranties the sequence of execution between threads.
Sample Code
using System;
using System.Threading;// Multithreading
class Program {
static void Main(string[] args) {
Console.WriteLine("Main Started");
var th1 = new Thread(Greet);
th1.Start(new object[] { "Bob", "Good Morning" });
var th2 = new Thread(Greet);
th2.Start(new object[] { "John", "Good Morning" });
Console.ReadKey();
}
private static void Greet(object info) {
Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
Console.WriteLine("\tThread Name: " + Thread.CurrentThread.Name);
Console.WriteLine("\tThread Id: " + Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("\tIs Background: " + Thread.CurrentThread.IsBackground);
Console.WriteLine("\tIs ThreadPool Thread: " + Thread.CurrentThread.IsThreadPoolThread);
Console.WriteLine("\tIs Alive: " + Thread.CurrentThread.IsAlive);
Console.WriteLine("\tPriority: " + Thread.CurrentThread.Priority);
Console.WriteLine("\tThread State: " + Thread.CurrentThread.ThreadState);
}
}
Output:
Main Started
Hi John, Good Morning!
Thread Name:
Thread Id: 11
Is Background: False
Is ThreadPool Thread: False
Is Alive: True
Hi Bob, Good Morning!
Thread Name:
Thread Id: 10
Is Background: False
Is ThreadPool Thread: False
Is Alive: True
Priority: Normal
Priority: Normal
Thread State: Running
Thread State: Running
No comments:
Post a Comment