package exemplo;

import java.util.HashSet;

public class GerenciadorConcorrencia {
    private HashSet chaves;

    public synchronized void iniciarExecucao(String key)  {
        try {
            while (!chaves.add(key)) {
                wait();
            }
        }
        catch(InterruptedException ex) {
            throw new RuntimeException("Falha na execução");
        }
        
    }    
    public synchronized void finalizarExecucao(String key) {
        try {
            if (!chaves.remove(key)) {
                throw new RuntimeException("Chave não encontrada");
            }
        }    
        finally {
            notifyAll();
        }
    }
}