Laboratorul 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lucrare de laborator nr 4

Varianta 2

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();

This study source was downloaded by 100000798432454 from CourseHero.com on 02-28-2022 04:23:23 GMT -06:00

https://www.coursehero.com/file/75224912/Laboratorul-4docx/
new Thread(consumer2).start();

new Thread(consumer3).start();

class Store{

ArrayList<Integer> depozit=new ArrayList<Integer>();

public synchronized void get(String str1) {

while (depozit.size()<1) {

try {

wait();

catch (InterruptedException e) {

System.out.println(str1+" a consumat: "+ depozit.get(depozit.size()-1));

depozit.remove(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();

This study source was downloaded by 100000798432454 from CourseHero.com on 02-28-2022 04:23:23 GMT -06:00

https://www.coursehero.com/file/75224912/Laboratorul-4docx/
}

public synchronized void put(String str,int m,int n) {

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();

This study source was downloaded by 100000798432454 from CourseHero.com on 02-28-2022 04:23:23 GMT -06:00

https://www.coursehero.com/file/75224912/Laboratorul-4docx/
class Producer extends Thread{

Store store;

Producer(Store store){

this.store=store;

public void run(){

int a,b;

for(int i=0;i<2;i++)

{a = (int)(Math.random()*100);

while (a%2==0)

{a++;}

b = (int)(Math.random()*100);

while (a%2==0)

{a++;}

store.put(getName(),a,b);}

}}

class Consumer extends Thread{

Store store;

Consumer(Store store){

this.store=store;

public void run(){

for(int i =0; i<2; i++)

{store.get(getName()); }

This study source was downloaded by 100000798432454 from CourseHero.com on 02-28-2022 04:23:23 GMT -06:00

https://www.coursehero.com/file/75224912/Laboratorul-4docx/
}}

This study source was downloaded by 100000798432454 from CourseHero.com on 02-28-2022 04:23:23 GMT -06:00

https://www.coursehero.com/file/75224912/Laboratorul-4docx/
Powered by TCPDF (www.tcpdf.org)

You might also like