Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Consumidor extends Thread{
- private ArrayList <Integer> lista;
- private int num;
- private Random random = new Random();
- public Consumidor(ArrayList <Integer> lista,int num) {
- this.lista=lista;
- this.num=num;
- }
- public void run() {
- int tiempoInicial=1000;
- int tiempoFinal=1500;
- while (true) {
- try {
- int tiempoRetardo = random.nextInt((tiempoFinal - tiempoInicial) + 1) + tiempoInicial;
- Thread.sleep(tiempoRetardo);
- synchronized (lista) {
- while (lista.isEmpty()) {
- System.out.println("Consumidor (" + num + "): Lista vacía. Esperando elementos...");
- lista.wait();
- }
- int elementoConsumido = lista.remove(0);
- System.out.println("Consumidor (" + num + ") consumió: " + elementoConsumido);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment