Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace Recetas.CSharp.Cap04.R0411
- {
- public sealed class UsoIncrementDecrement
- {
- // Valor de dato compartido entre threads:
- private static int variable = 0;
- public static void Main()
- {
- // Creación de dos threads:
- Thread t1 = new Thread(IncrementarDecrementar);
- Thread t2 = new Thread(IncrementarDecrementar);
- // Inicio de la ejecución de los dos threads:
- t1.Start();
- t2.Start();
- // Espera su finalización:
- t1.Join();
- t2.Join();
- // Muestra el valor final de `variable`:
- Console.WriteLine ("\nValor de `variable`: {0}\n", variable.ToString());
- }
- private static void IncrementarDecrementar()
- {
- // Incrementa y decrementa el valor de `variable`:
- Interlocked.Increment(ref variable);
- Interlocked.Decrement(ref variable);
- Interlocked.Decrement(ref variable);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement