29 0 36KB
A elaborat:stud gr. CR-191 FR A controlat: conf.univ.
Balan Ion Rotaru Lilia
Chisinau 2022
Lucrare de laborator nr 4
package com.company; import java.util.ArrayList; import java.lang.Math; public class Main { public static void main(String[] args) { Store store=new Store(); Producer producer = new Producer(store); producer.setName("Producatorul 1"); Producer producer1 = new Producer(store); producer1.setName("Producatorul 2"); Producer producer2 = new Producer(store); producer2.setName("Producatorul 3"); Consumer consumer = new Consumer(store); consumer.setName("Consumatorul 1"); Consumer consumer1 = new Consumer(store); consumer1.setName("Consumatorul 2"); Consumer consumer2 = new Consumer(store); consumer2.setName("Consumatorul 3"); Consumer consumer3 = new Consumer(store); consumer3.setName("Consumatorul 4"); new Thread(producer).start(); new Thread(producer1).start(); new Thread(producer2).start(); new Thread(consumer).start(); new Thread(consumer1).start(); new Thread(consumer2).start(); new Thread(consumer3).start(); } } class Store{ ArrayList depozit=new ArrayList(); public synchronized void get(String str1) { while (depozit.size()=4) { try { wait(); } catch (InterruptedException e) { } } System.out.print(str+" a adaugat 2 produse: "); depozit.add(m); System.out.print(depozit.get(depozit.size()-1)+", "); depozit.add(n); System.out.println(depozit.get(depozit.size()-1)); if(depozit.size()!=0){ System.out.print("Produse in depozit: " + depozit.size()+" --> "); for(Integer impar : depozit){ System.out.print(impar+ " "); } System.out.println(" "); } else{ System.out.println("In depozit nu sunt produse"); } notifyAll(); } } class Producer extends Thread{
Store store; Producer(Store store){ this.store=store; } public void run(){ { int a,b; for(int i=0;i