Skip to content

Instantly share code, notes, and snippets.

@everblut
Created February 24, 2012 21:20
Show Gist options
  • Save everblut/1903815 to your computer and use it in GitHub Desktop.
Save everblut/1903815 to your computer and use it in GitHub Desktop.
semaforoThread
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