0% found this document useful (0 votes)
58 views

Laboratorul 4

The document describes a program with producers that generate random odd numbers and add them to a shared store, and consumers that remove numbers from the store. The program creates three producer threads that add pairs of random numbers to the store, and four consumer threads that each remove two numbers from the store. A Store class synchronizes access to the shared array list that holds the numbers and notifies threads when items are added or removed.

Uploaded by

ion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Laboratorul 4

The document describes a program with producers that generate random odd numbers and add them to a shared store, and consumers that remove numbers from the store. The program creates three producer threads that add pairs of random numbers to the store, and four consumer threads that each remove two numbers from the store. A Store class synchronizes access to the shared array list that holds the numbers and notifies threads when items are added or removed.

Uploaded by

ion
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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