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 UsoAdd
- {
- // Valor de dato compartido entre threads:
- private static int variable;
- public static void Main()
- {
- // Creación de dos threads:
- Thread t1 = new Thread(Sumar);
- Thread t2 = new Thread(Sumar);
- // 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 Sumar()
- {
- // Suma 2 a `variable`:
- Interlocked.Add (ref variable, 2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement