Question

purpose of this 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 application will run with the welcome to war greeting :

here is an example of what it should look like .

run: Welcome to WAR Producer has started running Consumer has started running Producer produced 5 Buffer empty. Consumer wait

The buffer will be based on the SynchronizedBuffer but will store a Card instead of an int.

The producer 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.LinkedList;

public class Threadexample

{

    public static void main(String[] args)

                        throws InterruptedException

    {

        // Object of a class that has both produce()

        // and consume() methods

        final PC pc = new PC();

        // 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 PC

    {

        // 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);

                }

            }

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Purpose of this application is to create a multi-threaded application with one thread being a pro...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the last assignment, you created a card class. Modify the card class so the setValue()...

    In the last assignment, you created a card class. Modify the card class so the setValue() method does not allow a card’s value to be less than 1 or higher than 13. If the argument to setValue() is out of range, assign 1 to the card’s value. You also created a PickTwoCards application that randomly selects two playing cards and displays their values. In that application, all card objects were arbitrarily assigned a suit represented by a single character, but...

  • 1.A fair six-sided die is rolled. {1, 2, 3, 4, 5, 6} Let event A =...

    1.A fair six-sided die is rolled. {1, 2, 3, 4, 5, 6} Let event A = the outcome is greater than 4. Let event B = the outcome is an even number. Find P(A|B). A.0 B.1/3 C.2/3 C.3/3 2.A student stays at home.  Let event N = the student watches Netflix. Let event Y = the student watches the very educational youtube videos made by her/his instructor.Suppose P(N) = 0.1, P(Y) = 0.8, and P(N and Y) = 0.  Are N and...

  • MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans...

    MasterMind in Java Activity MasterMind project Create a new Java Application project named MasterMind allowing Netbeans IDE to create the main class called MasterMind.java MasterMind class Method main() should: Call static method System.out.println() and result in displaying “Welcome to MasterMind!” Call static method JOptionPane.showMessageDialog(arg1, arg2) and result in displaying a message dialog displaying “Let’s Play MasterMind!” Instantiate an instance of class Game() constants Create package Constants class Create class Constants Create constants by declaring them as “public static final”: public...

  • Java FX Application Purpose The purpose of this assignment is to get you familiar with the...

    Java FX Application Purpose The purpose of this assignment is to get you familiar with the basics of the JavaFX GUI interface components. This assignment will cover Labels, Fonts, Basic Images, and Layouts. You will use a StackPane and a BorderPane to construct the layout to the right. In addition you will use the Random class to randomly load an image when the application loads Introduction The application sets the root layout to a BorderPane. The BorderPane can be divided...

  • C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data...

    C++ Purpose: To practice arrays of records, and stepwise program development. 1. Define a record data type for a single playing card. A playing card has a suit ('H','D','S',or 'C'), and a value (1 through 13). Your record data type should have two fields: a suit field of type char, and a value field of type int. 2. In main(), declare an array with space for 52 records, representing a deck of playing cards. 3. Define a function called initialize()...

  • Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June...

    Please to indent and follow structure!!!!! Assignment 3 - The card game: War Due Date: June 9th, 2018 @ 23:55 Percentage overall grade: 5% Penalties: No late assignments allowed Maximum Marks: 10 Pedagogical Goal: Refresher of Python and hands-on experience with algorithm coding, input validation, exceptions, file reading, Queues, and data structures with encapsulation. The card game War is a card game that is played with a deck of 52 cards. The goal is to be the first player to...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

  • Create a Java text-based simulation that plays baccarat between two players. In baccarat, there i...

    Create a Java text-based simulation that plays baccarat between two players. In baccarat, there is a banker and there is a player. Two cards are dealt to the banker and two cards to the player. Both cards in each hand are added together, and the objective (player) is to draw a two or three-card hand that totals closer to 9 than the banker. In baccarat, the card values are as follows: • 2–9 are worth face value • 10, J,...

  • This needs to be done in c++11 and be compatible with g++ compiling Project description: Write...

    This needs to be done in c++11 and be compatible with g++ compiling Project description: Write a C++ program to simulate a simple card game between two players. The game proceeds as follows: The 52 cards in a deck of cards are shuffled and each player draws three cards from the top of the deck. Remaining cards are placed in a pile face-down between the two players. Players then select a card from the three in their hand. The player...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT