Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace Recetas.Multithreading.Cap01.R0107
- {
- public sealed class IteracionesThreads
- {
- public static void Main()
- {
- Console.Title = "Iteraciones Threads";
- Console.WriteLine ();
- // Creación de dos threads:
- Thread t1 = new Thread (
- new ParameterizedThreadStart (ContadorNumeros)
- );
- t1.Name = "ForegroundThread";
- Thread t2 = new Thread (
- new ParameterizedThreadStart (ContadorNumeros)
- );
- t2.Name = "BackgroundThread";
- // Hace que el thread `t2` sea de segundo plano:
- t2.IsBackground = true;
- // Número de iteraciones para cada thread:
- t1.Start(13);
- t2.Start(17);
- }
- public static void ContadorNumeros(object iteraciones)
- {
- int it = (int) iteraciones;
- for (int i = 1; i <= it; ++i)
- {
- // Suspende el thread 0.5 segundos:
- Thread.Sleep (TimeSpan.FromSeconds (0.5));
- Console.WriteLine ("El thread `{0}` imprime {1}",
- Thread.CurrentThread.Name,
- i.ToString()
- );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement