Java - codigo para baño segregado

 
Vista:

codigo para baño segregado

Publicado por Hazmyn (1 intervención) el 11/11/2018 20:11:06
Hola, me encuentro trabada y no se como crear los condicionales marcados en los comentarios

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package BanoSegregado;
 
import Utilities.*;
import Synchronization.*;
 
public class ControladorBano extends MyObject
{
    private int numtazas = 0;
    private final int LIBRE = 0, HOMBRES = 1, MUJERES = 2;
    private CountingSemaphore mujer = null;
    private CountingSemaphore hombre = null;
    private CountingSemaphore banio = null;
    private BinarySemaphore mutex = null;
    private CountingSemaphore usando = null;
    private int mujeresesperando=0;
    private int hombresesperando=0;
    private int personasenbanio=0;
    private int[] estado;
 
 
    public ControladorBano(int tazas)
    {
        numtazas = tazas;
        estado = new int[numtazas];
        for (int i = 0; i < numtazas; i++)
            estado[i] = LIBRE;
        mujer = new CountingSemaphore(0);
        hombre = new CountingSemaphore(0);
        banio = new CountingSemaphore(0);
        mutex = new BinarySemaphore(1);
        usando = new CountingSemaphore(0);
 
    }
 
    public void banoEsperandoPersonas()
    {
        System.out.println("tiempo=" + age() + ", Baño libre");
        if(estado[numtazas]==0) //si el baño esta libre y hay mujeres esperando entonces que le libere el semaforo de mujeresesperando
        {
            P(mujer);
            P(mutex);
            mujeresesperando--;
            V(banio);
            System.out.println("tiempo=" + age() +
                ", Baño tiene " + mujeresesperando + " mujeres esperando");
            V(mutex);
            System.out.println("tiempo=" + age() + ", Baño ocupado por mujeres");
            P(usando);
        }
        else
        {
            if (estado[numtazas]==0)//si el baño esta libre y hay hombres esperando entonces se libera el semaforo de hombresesperando
                 P(hombre);
            P(mutex);
            hombresesperando--;
            V(banio);
            System.out.println("tiempo=" + age() +
                ", Baño tiene " + hombresesperando + " hombres esperando");
            V(mutex);
            System.out.println("tiempo=" + age() + ", Baño ocupado por hombres");
            P(usando);
        }
    }
    public void mujeresEsperandoEntrar(int i, int tiempoEnBanio)
    {
        int espera;
        P(mutex);
        if(mujeresesperando < numtazas)
        {
            mujeresesperando++;
            System.out.println("tiempo=" + age() + ", Mujer " + i
                    + " esperando. Hay " + mujeresesperando
                    + " mujeres esperando");
            V(mujer);
            V(mutex);
            P(banio);
            espera = 1 + (int)random(tiempoEnBanio);
            System.out.println("tiempo=" + age() + ", Mujer " + i +
                    " va a necesitar " + espera + " ms para usar el baño");
            nap(espera);
            System.out.println("tiempo=" + age() + ", Mujer " + i +
                    " termino de usar el banio");
            V(usando);
        }
        else
        {
            System.out.println("tiempo=" + age() + ", Mujer " + i
                    + " encontro el baño lleno, hay " + mujeresesperando
                    + " mujeres esperando");
            V(mutex);
        }
    }
 
 
    public void hombresEsperandoEntrar(int j, int tiempoEnBanio)
    {
        int espera;
        P(mutex);
         if(hombresesperando < numtazas)
        {
            hombresesperando++;
            System.out.println("tiempo=" + age() + ", Hombre " + j
                    + " esperando. Hay " + hombresesperando
                    + " hombres esperando");
            V(hombre);
            V(mutex);
            P(banio);
            espera = 1 + (int)random(tiempoEnBanio);
            System.out.println("tiempo=" + age() + ", Hombre " + j +
                    " va a necesitar " + espera + " ms para usar el baño");
            nap(espera);
            System.out.println("tiempo=" + age() + ", Hombres " + j +
                    " termino de usar el banio");
            V(usando);
    }
       else
        {
            System.out.println("tiempo=" + age() + ", Hombres " + j
                    + " encontro el baño lleno, hay " + hombresesperando
                    + " hombres esperando");
            V(mutex);
        }
    }
}
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
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

codigo para baño segregado

Publicado por Billy Joel (874 intervenciones) el 12/11/2018 18:53:23
Podrías subir todo el código o proyecto para tratar de entenderlo bien
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar