Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace Recetas.Multithreading.Cap01.R0106
- {
- public class Priorizacion
- {
- private int contador;
- private Thread thread;
- public int Contador
- {
- get
- {
- return contador;
- }
- set
- {
- contador = value;
- }
- }
- public Thread Thread
- {
- get
- {
- return thread;
- }
- set
- {
- thread = value;
- }
- }
- public Priorizacion (string nombreThread)
- {
- contador = 0;
- thread = new Thread (Tarea);
- thread.Name = nombreThread;
- }
- public void Tarea()
- {
- Console.WriteLine ("\nEl thread `{0}' se está iniciando...",
- thread.Name
- );
- do
- {
- ++contador;
- Console.WriteLine ("Dentro del thread `{0}`.",
- thread.Name
- );
- } while (contador < 10000);
- Console.WriteLine ("\nEl thread `{0}' está finalizando...",
- thread.Name
- );
- }
- }
- public sealed class UsoThreadPriority
- {
- public static void Main()
- {
- // Creación de dos instancias de Priorizacion:
- Priorizacion p1 = new Priorizacion ("Alta Prioridad");
- Priorizacion p2 = new Priorizacion ("Baja Prioridad");
- // Establecimiento del nivel de prioridad:
- p1.Thread.Priority = ThreadPriority.AboveNormal;
- p2.Thread.Priority = ThreadPriority.Lowest;
- // Inicio de los threads:
- p1.Thread.Start();
- p2.Thread.Start();
- // Espera a que terminen:
- p1.Thread.Join();
- p2.Thread.Join();
- Console.WriteLine ();
- Console.WriteLine ("El thread `{0}` contó hasta: {1}", p1.Thread.Name, p1.Contador);
- Console.WriteLine ("El thread `{0}` contó hasta: {1}", p2.Thread.Name, p2.Contador);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement