Advertisement
Fhernd

UsoEnumThreadPriority.cs

Jul 10th, 2014
1,505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Recetas.Multithreading.Cap01.R0106
  5. {
  6.     public class Priorizacion
  7.     {
  8.         private int contador;
  9.         private Thread thread;
  10.        
  11.         public int Contador
  12.         {
  13.             get
  14.             {
  15.                 return contador;
  16.             }
  17.             set
  18.             {
  19.                 contador = value;
  20.             }
  21.         }
  22.        
  23.         public Thread Thread
  24.         {
  25.             get
  26.             {
  27.                 return thread;
  28.             }
  29.             set
  30.             {
  31.                 thread = value;
  32.             }
  33.         }
  34.        
  35.         public Priorizacion (string nombreThread)
  36.         {
  37.             contador = 0;
  38.             thread = new Thread (Tarea);
  39.             thread.Name = nombreThread;
  40.         }
  41.        
  42.         public void Tarea()
  43.         {
  44.             Console.WriteLine ("\nEl thread `{0}' se está iniciando...",
  45.                 thread.Name
  46.             );
  47.            
  48.             do
  49.             {
  50.                 ++contador;
  51.                
  52.                 Console.WriteLine ("Dentro del thread `{0}`.",
  53.                     thread.Name
  54.                 );
  55.             } while (contador < 10000);
  56.            
  57.             Console.WriteLine ("\nEl thread `{0}' está finalizando...",
  58.                 thread.Name
  59.             );
  60.         }
  61.     }
  62.    
  63.     public sealed class UsoThreadPriority
  64.     {
  65.         public static void Main()
  66.         {
  67.             // Creación de dos instancias de Priorizacion:
  68.             Priorizacion p1 = new Priorizacion ("Alta Prioridad");
  69.             Priorizacion p2 = new Priorizacion ("Baja Prioridad");
  70.            
  71.             // Establecimiento del nivel de prioridad:
  72.             p1.Thread.Priority = ThreadPriority.AboveNormal;
  73.             p2.Thread.Priority = ThreadPriority.Lowest;
  74.            
  75.             // Inicio de los threads:
  76.             p1.Thread.Start();
  77.             p2.Thread.Start();
  78.            
  79.             // Espera a que terminen:
  80.             p1.Thread.Join();
  81.             p2.Thread.Join();
  82.            
  83.             Console.WriteLine ();
  84.            
  85.             Console.WriteLine ("El thread `{0}` contó hasta: {1}", p1.Thread.Name, p1.Contador);
  86.             Console.WriteLine ("El thread `{0}` contó hasta: {1}", p2.Thread.Name, p2.Contador);
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement