Created
May 18, 2016 01:59
-
-
Save valterbarros/af6de98b047e4c2f9ba9611d384d6695 to your computer and use it in GitHub Desktop.
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
package quintaAula; | |
import java.util.ArrayList; | |
public class Fila extends Thread { | |
ArrayList<String> nomes = new ArrayList<>(); | |
public synchronized Object remove(int i) { | |
while (nomes.isEmpty()) { | |
try { | |
System.out.println("Tentando remover porém vazio esperando"); | |
this.wait(); // this.wait() | |
} catch (InterruptedException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
return nomes.remove(i); | |
} | |
public synchronized void add(String nome) { | |
System.out.println("Adicionando o elemento na lista " + nome); | |
nomes.add(nome); | |
notify(); // this.notifyAll() | |
} | |
@Override | |
public void run() { | |
this.add("Maria"); | |
this.add("Joao"); | |
this.impr(); | |
} | |
public void impr() { | |
for (String element : nomes) { | |
System.out.println("Element : " + element); | |
} | |
} | |
public static void main(String[] args) { | |
Fila td = new Fila(); | |
td.start(); | |
td.remove(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment