Created
July 13, 2011 22:56
-
-
Save mattmils/1081525 to your computer and use it in GitHub Desktop.
Soccorritori Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Soccorritori { | |
private static final int NUM = 4; // Numero soccorritori. | |
private static final int C = 3; // Capienza elicottero. | |
public static final int IDLE = 0; | |
public static final int SETTING = 1; | |
public static final int READY = 2; | |
public static final int BACK = 3; | |
public static void log(String text) { | |
System.out.println(Thread.currentThread().getName() + ": " + text); | |
} | |
public class Capo { | |
private int _state = IDLE; | |
private Elicottero _elicottero; | |
public Capo(Elicottero elicottero) { | |
this._elicottero = elicottero; | |
} | |
public synchronized void saliPilota(Pilota pilota) { | |
// Il pilota sale | |
while (_state != IDLE) { | |
try { | |
wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
log("Pilota salito a bordo."); | |
_state = SETTING; | |
notifyAll(); | |
} | |
public synchronized void iniziaMissione() { | |
while (_state != READY) { | |
try { | |
log("Pilota attende che l'elicottero sia pieno."); | |
wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
log("Missione iniziata."); | |
notifyAll(); | |
} | |
public synchronized void terminaMissione() { | |
long time = (long) (Math.random() * 10000); | |
try { | |
Thread.sleep(time); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
_state = BACK; | |
log("Missione durata " + time + " millis"); | |
notifyAll(); | |
} | |
public synchronized void saliBordo(Soccorritore soccorritore) { | |
while (_state != SETTING || _elicottero.isPieno()) { | |
try { | |
log("Soccorritore in attesa."); | |
wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
// Fai salire il soccorritore sull'elicottero. | |
_elicottero.getSoccorritori().add(soccorritore); | |
log("Soccorritore salito a bordo."); | |
if (_elicottero.isPieno()) { | |
_state = READY; | |
} | |
// Notifica agli altri thread. | |
notifyAll(); | |
} | |
public synchronized void scendi(Soccorritore soccorritore) { | |
while (_state != BACK) { | |
try { | |
log("Soccorritore in missione."); | |
wait(); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
_elicottero.getSoccorritori().remove(soccorritore); | |
log("Soccorritore sceso."); | |
if (_elicottero.isVuoto()) | |
_state = IDLE; | |
notifyAll(); | |
} | |
} | |
public class Pilota implements Runnable { | |
private final Capo _capo; | |
public Pilota(Capo capo) { | |
this._capo = capo; | |
} | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
Thread.sleep((long) (Math.random() * 5000)); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
// Ricevuto allarme. | |
_capo.saliPilota(this); | |
_capo.iniziaMissione(); | |
_capo.terminaMissione(); | |
} | |
} | |
} | |
public class Elicottero { | |
private boolean _partito; | |
private final int _max; | |
private boolean _pilota = false; | |
private List<Soccorritore> _soccorritori; | |
public Elicottero(int max) { | |
// TODO Auto-generated constructor stub | |
this._max = max; | |
this._soccorritori = new ArrayList<Soccorritori.Soccorritore>(); | |
} | |
public boolean isPieno() { | |
return this._max == _soccorritori.size(); | |
} | |
public boolean isVuoto() { | |
return _soccorritori.size() == 0; | |
} | |
public List<Soccorritore> getSoccorritori() { | |
return _soccorritori; | |
} | |
public int getMax() { | |
return _max; | |
} | |
public boolean partito() { | |
return _partito; | |
} | |
public boolean pilotaBordo() { | |
return _pilota; | |
} | |
public void pilotaSali() { | |
_pilota = true; | |
} | |
public void pilotaScendi() { | |
_pilota = false; | |
} | |
public void iniziaMissione() { | |
_partito = true; | |
} | |
public void terminaMissione() { | |
_partito = false; | |
} | |
} | |
public class Soccorritore implements Runnable { | |
private final Capo _capo; | |
public Soccorritore(Capo capo) { | |
this._capo = capo; | |
} | |
@Override | |
public void run() { | |
while (true) { | |
this._capo.saliBordo(this); | |
this._capo.scendi(this); | |
try { | |
Thread.sleep((long) (Math.random() * 1000)); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
public Soccorritori() { | |
Elicottero elicottero = new Elicottero(C); | |
Capo capo = new Capo(elicottero); | |
for (int i = 0; i < NUM; i++) { | |
Thread thread = new Thread(new Soccorritore(capo)); | |
thread.setName("Soccorritore " + i); | |
thread.start(); | |
} | |
Thread pilota = new Thread(new Pilota(capo)); | |
pilota.setName("Pilota"); | |
pilota.start(); | |
} | |
public static void main(String[] args) { | |
new Soccorritori(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment