Skip to content

Instantly share code, notes, and snippets.

@mattmils
Created July 25, 2011 22:12
Show Gist options
  • Save mattmils/1105377 to your computer and use it in GitHub Desktop.
Save mattmils/1105377 to your computer and use it in GitHub Desktop.
//Marzorati Claudio
//726748
public class Squadra {
private static int atleti=3,id;
public static void main(String[] args) {
Allenatore a = new Allenatore();
for(int i=0;i<atleti;i++)
new Rematore(id,a).start();
}
}
public class Allenatore {
Rematore r;
public Allenatore(){
}
int nrematori=0,nremate=0;
boolean inizia=false;
public void arriva(Rematore r) {
while(nrematori<2)
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();}
nrematori++;
if (nrematori==2)
System.out.println("Ci sono i 2 rematori");
notifyAll();
}
public void parti(Rematore r) {
if (nrematori==2)
System.out.println("Partiti");
inizia=true;
}
public void rema(Rematore r) {
while((inizia==true)&&(nremate<3))
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();}
nremate++;
if(nremate==3)
System.out.println("Han fatto le 3 remate");
notifyAll();
}
public void cena(Rematore r) {
if(nremate==3)
System.out.println("Buon appetito");
}
}
public class Rematore extends Thread{
Allenatore a;
private Rematore r;
int id;
public Rematore(int id,Allenatore a){
this.id=id;
this.a=a;
}
public void run(){
a.arriva(r);
a.parti(r);
a.rema(r);
a.cena(r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment