0% found this document useful (0 votes)
41 views2 pages

Java Multithreading Producer-Consumer Example

The document defines 4 Java classes - Consumer, Producer, Lab1Main, and Shop - that simulate a producer-consumer problem. The Producer class generates random items and adds them to the Shop's inventory until it reaches the limit. The Consumer class repeatedly removes items from the Shop. The Lab1Main class creates instances of Producer and Consumer and starts their threads. The Shop class represents the shared inventory, synchronized to prevent concurrent access issues. It tracks the current items and ensures adds and removes only occur if space is available.

Uploaded by

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

Java Multithreading Producer-Consumer Example

The document defines 4 Java classes - Consumer, Producer, Lab1Main, and Shop - that simulate a producer-consumer problem. The Producer class generates random items and adds them to the Shop's inventory until it reaches the limit. The Consumer class repeatedly removes items from the Shop. The Lab1Main class creates instances of Producer and Consumer and starts their threads. The Shop class represents the shared inventory, synchronized to prevent concurrent access issues. It tracks the current items and ensures adds and removes only occur if space is available.

Uploaded by

Mihail Boran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Consumer

public class Consumer extends Thread {


int i;
@Override
public void run() {
while (i<20){
Shop.removeItem();
try {
Consumer.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
}
}
}

Lab1
public class Lab1Main {
public static void main(String[] args) {
Producer producer4=new Producer();
Producer producer1=new Producer();
Producer producer2=new Producer();
Producer producer3=new Producer();

//Consumer consumer=new Consumer();


Consumer consumer1=new Consumer();
Consumer consumer2=new Consumer();
Consumer consumer3=new Consumer();

producer1.start();
producer2.start();
producer3.start();
producer4.start();
consumer1.start();
consumer2.start();
consumer3.start();
}
}

Producer

import java.util.Random;

public class Producer extends Thread {


// static Random randomNumber=new Random();
static int producerCount;
final int producerLimit=42;

static int count;

@Override
public void run() {
while (count<producerLimit){
if(Shop.addItem(new Random().nextInt(10))){
count++;
}
try {
Producer.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
}
}

Shop
import java.util.ArrayList;
import java.util.List;

public class Shop {


static int space;
static int i=1;
final static int SPACE_LIMIT=5;
static List<Integer> shopList=new ArrayList<>();

static synchronized boolean isEmpty(){


if (shopList.size()==0)
return true;
else
return false;
}

static synchronized boolean isFull(){


if(shopList.size()==SPACE_LIMIT)
return true;
else
return false;
}

static synchronized boolean addItem(int item){


if(!isFull()) {
shopList.add(item);
System.out.println("№"+i++ + " " +item + " was added");
return true;
}else {
System.out.println("Cannot add item, Shop is full");
return false;
}
}
static synchronized boolean removeItem(){
if(!isEmpty()) {
System.out.println(shopList.get(shopList.size()-1) + " was taken");
shopList.remove(shopList.size()-1);
return true;
}else {
System.out.println(" ");
return false;
}
}
}

You might also like