Untitled

 avatar
unknown
plain_text
5 months ago
4.4 kB
4
Indexable
import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
class Depozit {
    private final Queue<Character> buffer = new LinkedList<>();
    private final int capacitate;
    private boolean mesajDepozitGolAfișat = false;
    public synchronized char[] preia(int obiecteRamase) throws InterruptedException {
        while (buffer.size() < 2) {
            if (obiecteRamase <= 0) {
                return new char[0]; 
            }
            System.out.println("Depozitul este gol, consumatorul așteaptă...");
            wait();
        }
        char[] obiecte = {buffer.poll(), buffer.poll()};
        System.out.println("Consumatorul a preluat: [" + obiecte[0] + ", " + obiecte[1] + "]");
        notifyAll();
        return obiecte;
    }
    
    public Depozit(int capacitate) {
        this.capacitate = capacitate;
    }

    public synchronized void pune(char[] obiecte) throws InterruptedException {
        for (char obiect : obiecte) {
            while (buffer.size() == capacitate) {
                System.out.println("Depozitul este plin, producătorul așteaptă...");
                wait();
            }
            buffer.add(obiect);
            mesajDepozitGolAfișat = false; 
            System.out.println("Producătorul a adăugat: " + obiect);
            notifyAll();
        }
    }

    public synchronized char[] preia() throws InterruptedException {
        while (buffer.size() < 2) {
            if (!mesajDepozitGolAfișat) {
                System.out.println("Depozitul este gol, consumatorul așteaptă...");
                mesajDepozitGolAfișat = true; 
            }
            wait();
        }
        char[] obiecte = {buffer.poll(), buffer.poll()};
        System.out.println("Consumatorul a preluat: [" + obiecte[0] + ", " + obiecte[1] + "]");
        notifyAll();
        return obiecte;
    }
}

class Producator extends Thread {
    private final Depozit depozit;
    private final Random random = new Random();

    public Producator(Depozit depozit) {
        this.depozit = depozit;
    }

    private char[] genereazaDouaConsoane() {
        char[] consoane = "BCDFGHJKLMNPQRSTVWXYZ".toCharArray();
        return new char[]{
            consoane[random.nextInt(consoane.length)],
            consoane[random.nextInt(consoane.length)]
        };
    }

    @Override
    public void run() {
        try {
            while (true) {
                char[] obiecte = genereazaDouaConsoane();
                depozit.pune(obiecte);
                Thread.sleep(random.nextInt(100));
            }
        } catch (InterruptedException e) {
            System.out.println("Producătorul a fost întrerupt.");
        }
    }
}

class Consumator extends Thread {
    private final Depozit depozit;
    private final int obiecteNecesare; 
    private int obiecteConsumate = 0;

    public Consumator(Depozit depozit, int obiecteNecesare) {
        this.depozit = depozit;
        this.obiecteNecesare = obiecteNecesare;
    }

    @Override
    public void run() {
        try {
            while (true) {
                synchronized (depozit) {
                    if (obiecteConsumate >= obiecteNecesare) {
                        break;
                    }
                }
                char[] pereche = depozit.preia(obiecteNecesare - obiecteConsumate);
                if (pereche.length == 0) {
                    break; 
                }
                obiecteConsumate += pereche.length;
                Thread.sleep(100);
            }
            System.out.println("Consumatorul a terminat. Obiecte consumate: " + (obiecteConsumate + 6));
        } catch (InterruptedException e) {
            System.out.println("Consumatorul a fost întrerupt.");
        }
    }
}




public class ProducatorConsumator {
    public static void main(String[] args) {
        int dimensiuneDepozit = 11;
        int obiecteNecesareConsumatori = 6;

        Depozit depozit = new Depozit(dimensiuneDepozit);

        Producator p1 = new Producator(depozit);
        Producator p2 = new Producator(depozit);
        Producator p3 = new Producator(depozit);

        Consumator c1 = new Consumator(depozit, obiecteNecesareConsumatori);
        Consumator c2 = new Consumator(depozit, obiecteNecesareConsumatori);

        p1.start();
        p2.start();
        p3.start();

        c1.start();
        c2.start();
    }
}
Editor is loading...
Leave a Comment