Created
June 26, 2014 21:46
-
-
Save Fhernd/a4231c7457b27e41782c to your computer and use it in GitHub Desktop.
Demostración del uso de la versión sobrecargada Join(Int32) en C#.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
namespace Recetas.Multithreading.Cap01 | |
{ | |
internal sealed class UsoThreadJoinInt32 | |
{ | |
// 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(); | |
} | |
// Este método es el encargado de llevar a cabo | |
// la tarea de ejecución de threads: | |
private static void ProcesoThread() | |
{ | |
Console.WriteLine ("\nThread en curso: {0}", Thread.CurrentThread.Name); | |
if (Thread.CurrentThread.Name.Equals ("Thread1") && | |
thread2.ThreadState != ThreadState.Unstarted) | |
{ | |
// Espera a por 2 segundos a que `thread2` termine: | |
if (thread2.Join (2000)) | |
{ | |
Console.WriteLine ("`thread2` ha terminado su ejecución."); | |
} | |
else | |
{ | |
Console.WriteLine ("El tiempo de espera ha transcurrido el `thread1` continuará su ejecución."); | |
} | |
} | |
// 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment