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

Program 7

The document describes a Java program for a fruit market problem using multithreading. Farmers can produce fruits and put them in a market with limited capacity, consumers can purchase fruits from the market.

Uploaded by

PRIYANSHU KUMARI
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)
8 views

Program 7

The document describes a Java program for a fruit market problem using multithreading. Farmers can produce fruits and put them in a market with limited capacity, consumers can purchase fruits from the market.

Uploaded by

PRIYANSHU KUMARI
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

Multithreading

7. Design and develop a Java program for the fruit market problem. The farmer will be able to
produce different types of fruits (apple, orange, grape, and watermelon), and put them in the
market to sell. The market has limited capacity and farmers have to stand in a queue if the
capacity is exceeded to sell their fruits. Consumers can come to the market any time and
purchase their desired fruits; and if the fruits they want to buy runs out, they are willing to
wait until the supply of that kind is ready. Examine and formulate an approach to address this
problem and implement the same using Java constructs for programming.

import java.util.LinkedList;
import java.util.Random;

class Market
{
LinkedList<String> Fruitslist = new LinkedList<>();
int capacity=10;
String[] fruits = {"orange","grapes","apple","mango","watermelon"};

Random random = new Random();

public synchronized void produce() throws InterruptedException


{
while (true) {
synchronized (this) {
while (Fruitslist.size() == capacity)
wait();
String fruit = fruits[random.nextInt(4)];
System.out.println("Fruit " + fruit + " is added!!!");
Fruitslist.add(fruit);
notifyAll();
Thread.sleep(500);
}
}
}

public synchronized String consume() throws InterruptedException {


while (true) {
synchronized (this)
{
while (Fruitslist.size() == 0)
wait();

String fruit = Fruitslist.removeFirst();


System.out.println("Fruit " + fruit + " is consumed!!!");
notifyAll();
Thread.sleep(500);
}
}
}
}

public class Program7{


public static void main(String[] args) throws InterruptedException
{
Market SuperMarket = new Market();
Thread produce = new Thread(()->{
try
{
for(int i=0; i<10; i++) {
SuperMarket.produce();
}
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
});

Thread consume = new Thread(()->{


try
{
for(int i=0; i<10; i++) {
SuperMarket.consume();
}
Thread.sleep(500);
}
catch(InterruptedException e)
{
System.out.println(e);
}
});
produce.start();
consume.start();
produce.join();
consume.join();
}
}

Output

You might also like