Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a java application need to create a multi-threaded application with one thread b

ID: 3812667 • Letter: A

Question

a java application need 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 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

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.

Explanation / Answer

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

                }

            }

        }

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote