Una clase Thread es responsable de crear y administrar un hilo en la programación multihilo. Proporciona una propiedad conocida como ThreadState para verificar el estado actual del hilo. El estado inicial de un subproceso es el estado Unstarted .
Sintaxis:
public ThreadState ThreadState{ get; }
Valor de retorno: esta propiedad devuelve el valor que indica el estado del hilo actual.
Los siguientes programas ilustran el uso de la propiedad ThreadState :
Ejemplo 1:
// C# program to illustrate the // use of ThreadState property using System; using System.Threading; public class GFG { // Main Method static public void Main() { Thread thr; // Get the reference of main Thread // Using CurrentThread property thr = Thread.CurrentThread; // Display the current state // of the main thread Console.WriteLine("The name of the current state of the Main " + "thread is: {0}", thr.ThreadState); } }
Producción:
The name of the current state of the main thread is: Running
Ejemplo 2:
// C# program to illustrate the // use of ThreadState property using System; using System.Threading; public class GFG { // Main method public static void Main() { // Creating and initializing threads Thread TR1 = new Thread(new ThreadStart(job)); Thread TR2 = new Thread(new ThreadStart(job)); Console.WriteLine("ThreadState of TR1 thread"+ " is: {0}", TR1.ThreadState); Console.WriteLine("ThreadState of TR2 thread"+ " is: {0}", TR2.ThreadState); // Running state TR1.Start(); Console.WriteLine("ThreadState of TR1 thread "+ "is: {0}", TR1.ThreadState); TR2.Start(); Console.WriteLine("ThreadState of TR2 thread"+ " is: {0}", TR2.ThreadState); } // Static method public static void job() { Thread.Sleep(2000); } }
Producción:
ThreadState of TR1 thread is: Unstarted ThreadState of TR2 thread is: Unstarted ThreadState of TR1 thread is: Running ThreadState of TR2 thread is: WaitSleepJoin
Referencia:
Publicación traducida automáticamente
Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA