Clase Thread - docencia de la ETSIT-URJC
Clase Thread - docencia de la ETSIT-URJC
Clase Thread - docencia de la ETSIT-URJC
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
public c<strong>la</strong>ss Hucha {<br />
private int ahorros = 0; private final int MAXIMO = 10;<br />
public synchronized void mete (int cantidad) {<br />
while (ahorros + cantidad > MAXIMO)<br />
try {wait();} catch (InterruptedException e) {return;};;<br />
ahorros = ahorros + cantidad;<br />
notifyAll(); // Despertamos a los gastadores, y al resto<br />
}<br />
public synchronized void saca (int cantidad) {<br />
while (ahorros < cantidad)<br />
try {wait();} catch (InterruptedException e) {return;};<br />
ahorros = ahorros - cantidad;<br />
notifyAll(); // Despertamos a los ahorradores, y al resto<br />
}<br />
public synchronized int ahorros () { return ahorros; }<br />
}<br />
public c<strong>la</strong>ss Gastador extends <strong>Thread</strong> {<br />
Hucha hucha; int iteraciones;<br />
public Gastador<br />
( String nombre, Hucha hucha, int iteraciones ) {<br />
super (nombre);<br />
this.hucha = hucha;<br />
this.iteraciones = iteraciones;<br />
}<br />
public void run () {<br />
for (int i=1; i