Created
February 24, 2012 21:20
-
-
Save everblut/1903815 to your computer and use it in GitHub Desktop.
semaforoThread
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
public class semaforoThread extends Thread { | |
private Boolean[] colores; | |
private Boolean nonStop; | |
private char identificador; | |
/*Verde,Amarillo,Rojo*/ | |
public semaforoThread(char identificador){ | |
colores = new Boolean[3]; | |
colores[0] = false; | |
colores[1] = false; | |
colores[2] = false; | |
nonStop = true; | |
this.identificador = identificador; | |
} | |
public void setVerde(){ | |
colores[0] = true; | |
colores[1] = false; | |
colores[2] = false; | |
} | |
public void setAmarillo(){ | |
colores[0] = false; | |
colores[1] = true; | |
colores[2] = false; | |
} | |
public void setRojo(){ | |
colores[0] = false; | |
colores[1] = false; | |
colores[2] = true; | |
} | |
public void setOff(){ | |
colores[0] = false; | |
colores[1] = false; | |
colores[2] = false; | |
} | |
public synchronized String getValue(){ | |
if(colores[0] == true){ | |
return "verde"; | |
}else if(colores[1] == true){ | |
return "amarillo"; | |
}else { | |
return "rojo"; | |
} | |
} | |
public void termina(){ | |
nonStop = false; | |
} | |
public void run(){ | |
while(nonStop){ | |
try{ | |
Thread.sleep(500); | |
System.out.println("Soy "+identificador+" y estoy en "+getValue()); | |
}catch(InterruptedException e){ | |
System.out.println("Interrumpiado :c"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment