Commit 10d3eada by Pedro J Sanchez

Upload New File

parent 96be4d08
package es.uja.cursojee.simulaeventos;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Registro {
private final Evento evento;
private final List<Usuario> listaAsistentes;
private final List<Usuario> listaEspera;
private final Lock exm;
// Constantes
private final int PRIMERO = 0;
public Registro(Evento evento) {
super();
this.evento = evento;
this.listaAsistentes = new ArrayList();
this.listaEspera = new ArrayList();
this.exm = new ReentrantLock();
}
public Evento getEvento() {
return evento;
}
public boolean addAsistente(Usuario asistente) {
boolean resultado = false;
// Protocolo entrada exm
exm.lock();
try {
// Sección crítica
if( listaAsistentes.size() < evento.getAforo() ) {
listaAsistentes.add(asistente);
resultado = true;
} else {
listaEspera.add(asistente);
}
} finally {
// Protocolo salida exm
exm.unlock();
}
return resultado;
}
public boolean removeAsistente(Usuario asistente) {
boolean resultado = false;
// Protocolo entrada exm
exm.lock();
try {
// Sección crítica exm
if( listaAsistentes.remove(asistente) ) {
if( !listaEspera.isEmpty() ) {
Usuario espera = listaEspera.remove(PRIMERO);
listaAsistentes.add(espera);
}
resultado = true;
}
} finally {
// Protoco salida exm
exm.unlock();
}
return resultado;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment