Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created June 26, 2014 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/a4231c7457b27e41782c to your computer and use it in GitHub Desktop.
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#.
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