C sharp - multihilos

 
Vista:
sin imagen de perfil
Val: 4
Ha aumentado su posición en 7 puestos en C sharp (en relación al último mes)
Gráfica de C sharp

multihilos

Publicado por francisco antonio (4 intervenciones) el 31/03/2019 06:13:45
porque me marca error el metodo ingreso
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace proyhilobanco
{
    class Program
    {
        public class cuenta
        {
            private Object bloqueo = new Object();
            private int saldo;
            Random alealtorio = new Random();
            public cuenta(int saldoini)
            {
                saldo = saldoini;
 
            }
            public int Retiro(int cantidad)
            {
                if (saldo < 0)
                {
                    throw new Exception("Saldo Negativo.");
                }
 
                // Sección crítica. Sólo se permite una transacción 
                // de retiro de forma simultánea:
                lock (bloqueo)
                {
                    if (saldo >= cantidad)
                    {
                        Console.WriteLine("Saldo antes del retiro: {0}", saldo.ToString());
                        Console.WriteLine("Cantidad a retirar: -{0}", cantidad.ToString());
                        saldo -= cantidad;
                        Console.WriteLine("Saldo después del retiro: {0}", saldo.ToString());
                        return cantidad;
                    }
                    else
                    {
                        return 0;
                    }
                }
 
 
            }
            public int Ingreso(int cantidad)
            {
                if (saldo > 0)
                {
                    lock (bloqueo)
                    {
                        if (saldo >= cantidad)
                        {
                            Console.WriteLine("saldo antes del deposito: {0}", saldo.ToString());
                            Console.WriteLine("Cantidad a depositar: +{0}", saldo.ToString());
                            saldo += cantidad;
                            Console.WriteLine("Saldo después del deposito: {0}", saldo.ToString());
                            return cantidad;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
            }
            public void RealizarTransaccion()
            {
                for (int i = 1; i <= 10; ++i)
                {
                    // Realiza retiros entre 1 y 100 unidades monetarias:
                    Retiro(alealtorio.Next(1, 11));
                }
            }
        }
 
        static void Main(string[] args)
        {
            Thread[] threads = new Thread[10];
            cuenta c = new cuenta(1000);
            for (int i = 0; i < 10; ++i)
            {
                Thread t = new Thread(new ThreadStart(c.RealizarTransaccion));
                threads[i] = t;
            }
            // Inicia los threads para ejecución simultánea:
            for (int i = 0; i < 10; ++i)
            {
                threads[i].Start();
            }
            Console.WriteLine();
            Console.ReadKey();
        }
 
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder