Lucrare de laborator nr 4
Varianta 2
package [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Store store=new Store();
Producer producer = new Producer(store);
[Link]("Producatorul 1");
Producer producer1 = new Producer(store);
[Link]("Producatorul 2");
Producer producer2 = new Producer(store);
[Link]("Producatorul 3");
Consumer consumer = new Consumer(store);
[Link]("Consumatorul 1");
Consumer consumer1 = new Consumer(store);
[Link]("Consumatorul 2");
Consumer consumer2 = new Consumer(store);
[Link]("Consumatorul 3");
Consumer consumer3 = new Consumer(store);
[Link]("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 [Link] on 02-28-2022 [Link] GMT -06:00
[Link]
new Thread(consumer2).start();
new Thread(consumer3).start();
class Store{
ArrayList<Integer> depozit=new ArrayList<Integer>();
public synchronized void get(String str1) {
while ([Link]()<1) {
try {
wait();
catch (InterruptedException e) {
[Link](str1+" a consumat: "+ [Link]([Link]()-1));
[Link]([Link]()-1);
if([Link]()!=0){
[Link]("Produse in depozit: " + [Link]()+" --> ");
for(Integer impar : depozit){
[Link](impar+ " ");
[Link](" ");}
else{
[Link]("In depozit nu sunt produse");
notifyAll();
This study source was downloaded by 100000798432454 from [Link] on 02-28-2022 [Link] GMT -06:00
[Link]
}
public synchronized void put(String str,int m,int n) {
while ([Link]()>=4) {
try {
wait();
catch (InterruptedException e) {
[Link](str+" a adaugat 2 produse: ");
[Link](m);
[Link]([Link]([Link]()-1)+", ");
[Link](n);
[Link]([Link]([Link]()-1));
if([Link]()!=0){
[Link]("Produse in depozit: " + [Link]()+" --> ");
for(Integer impar : depozit){
[Link](impar+ " ");
[Link](" ");}
else{
[Link]("In depozit nu sunt produse");
notifyAll();
This study source was downloaded by 100000798432454 from [Link] on 02-28-2022 [Link] GMT -06:00
[Link]
class Producer extends Thread{
Store store;
Producer(Store store){
[Link]=store;
public void run(){
int a,b;
for(int i=0;i<2;i++)
{a = (int)([Link]()*100);
while (a%2==0)
{a++;}
b = (int)([Link]()*100);
while (a%2==0)
{a++;}
[Link](getName(),a,b);}
}}
class Consumer extends Thread{
Store store;
Consumer(Store store){
[Link]=store;
public void run(){
for(int i =0; i<2; i++)
{[Link](getName()); }
This study source was downloaded by 100000798432454 from [Link] on 02-28-2022 [Link] GMT -06:00
[Link]
}}
This study source was downloaded by 100000798432454 from [Link] on 02-28-2022 [Link] GMT -06:00
[Link]
Powered by TCPDF ([Link])