Skip to content

Instantly share code, notes, and snippets.

@valterbarros
Created May 18, 2016 01:59
Show Gist options
  • Save valterbarros/af6de98b047e4c2f9ba9611d384d6695 to your computer and use it in GitHub Desktop.
Save valterbarros/af6de98b047e4c2f9ba9611d384d6695 to your computer and use it in GitHub Desktop.
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