using System; using System.Threading; namespace Recetas.Multithreading.Cap01 { internal sealed class UsoThreadJoin { // Declaración de threads: private static Thread threadMain, thread1, thread2; public static void Main() { threadMain = Thread.CurrentThread; thread1 = new Thread (ProcesoThread); // El nombre facilita su identificación: thread1.Name = "Thread1"; thread1.Start(); // Crea una instancia de Thread: thread2 = new Thread (ProcesoThread); // El nombre facilita su identificación: thread2.Name = "Thread2"; thread2.Start(); } // Método compatible con la firma del delegado // ThreadStart: private static void ProcesoThread() { Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name); if (Thread.CurrentThread.Name.Equals ("Thread1") && thread2.ThreadState != ThreadState.Unstarted) { // Pone en espera el thread actual hasta // que el thread sobre el que se invoca Join finaliza: thread2.Join (); } // Pausa el thread por 4 segundos: Thread.Sleep (4000); Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name); Console.WriteLine ("`thread1`: {0}", thread1.ThreadState); Console.WriteLine ("`thread2`: {0}\n", thread2.ThreadState); } } }