The purpose of this java application is to create a multi-threaded application w
ID: 3813255 • Letter: T
Question
The purpose of this java application is to create a multi-threaded application with one thread being a producer of playing cards and another thread being a consumer of playing cards.
The buffer will be based on the SynchronizedBuffer but will store a Card instead of an int.
The producer (Chapter 23, Fig. 23.10, pg. 979) will produce a playing card having a random value of either 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, or Ace. The producer will put that randomly created card in the buffer. Afterward, the producer will sleep randomly between 1-4 seconds before producing another card.
The consumer will get 2 cards from the buffer. Once the consumer has 2 cards, it will use them to simulate the card game WAR. The consumer will compare the cards and print the winner. Afterward, the consumer will sleep randomly between 1-4 seconds before consuming cards and simulating the game again.
Both producer and consumer will run forever. This means the output of the game will never end – the game will play forever.
Watch a demonstration of the application:
https://youtu.be/MDpKPx0FBcc
General Requirements
Your application must follow these general requirements
All classes must be in a package following these rules:
The package name is your last name plus the first letter of your first name. For example, if you name is Rita Red, her package is “red.r”
Package names are all lowercase letters
All class names must start with an UPPERCASE letter then camel-cased after that.
All property names must start with a lowercase letter then came-cased after that.
All method names must start with a lowercase letter then came-cased after that.
Output must match the examples. Watch out for spaces and punctuation.
GradinG
NOTE: If the code does not compile, it’s an automatic 0!
The package name is your last name plus the first letter of your first name. For example, if you name is Rita Red, her package is “red.r”
5
Print welcome message
5
Create a buffer
5
Buffer can only hold 1 card at a time
5
Start producer running
5
Start consumer running
5
Producer randomly creates a card and puts it in the buffer.
5
Producer randomly sleeps 1-4 seconds
5
Consumer reads 2 cards from buffer
5
Consumer plays the WAR game and shows the results.
5
Consumer randomly sleeps 1-4 seconds
5
Producer and consumer loop forever
5
The package name is your last name plus the first letter of your first name. For example, if you name is Rita Red, her package is “red.r”
5
Print welcome message
5
Create a buffer
5
Buffer can only hold 1 card at a time
5
Start producer running
5
Start consumer running
5
Producer randomly creates a card and puts it in the buffer.
5
Producer randomly sleeps 1-4 seconds
5
Consumer reads 2 cards from buffer
5
Consumer plays the WAR game and shows the results.
5
Consumer randomly sleeps 1-4 seconds
5
Producer and consumer loop forever
5
Explanation / Answer
package com.chegg.test;
//Java program to implement solution of producer
//consumer problem.
import java.util.LinkedList;
public class Threadexample
{
public static void main(String[] args)
throws InterruptedException
{
System.out.println("Welcome to Chegg....");
// Object of a class that has both produce()
// and consume() methods
final ProducerConsumer pc = new ProducerConsumer();
// Create producer thread
Thread t1 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable()
{
@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();
// t1 finishes before t2
t1.join();
t2.join();
}
// This class has a list, producer (adds items to list
// and consumber (removes items).
public static class ProducerConsumer
{
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// producer thread waits while list
// is full
while (list.size()==capacity)
wait();
System.out.println("Producer produced-"
+ value);
// to insert the jobs in the list
list.add(value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
Thread.sleep(1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list
// is empty
while (list.size()==0)
wait();
//to retrive the ifrst job in the list
int val = list.removeFirst();
System.out.println("Consumer consumed-"
+ val);
// Wake up producer thread
notify();
// and sleep
Thread.sleep(1000);
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.