Question & Answer: Modify the following code using multi-threading Java implementation of the system……

Modify the following code using multi-threading Java implementation of the system.

At the minimum:

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Modify the following code using multi-threading Java implementation of the system……
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

a. Each station should be implemented using a dedicated thread

b. The simulation control should be implemented using a dedicated thread.

c. The carrier should be a shared resource (as the bounded Q in the threads example).

d. Threads should have provisions for synchronization, avoidance of race conditions and deadlocks.

e. Threads cannot communicate with a other threads via inter-process communication (except for using locks and synch.)

The program should use the following class diagram.
Simulation +main(args: Stringl): void +check station_status(): void tupdate_station_status(): void +update_carrier_status(): void Class Diagram Station Carrier -station status: boolean probability double -R2_start transmission: ticks -R3_delay1: ticks -R4_delay2: ticks Set/get parameters R5_carrier length: met +set/get parameters


//CSMACD Class
import java.util.Random;
public class CSMACD
{

    static boolean randomBool()
    {
        Random ran=new Random();
        int num=ran.nextInt();
        return num%2==1;
    }
    public static void main(String[] args)
    {
        Station s1,s2,s3;
        double r1, r2 , r3, r4;
        boolean carrierStatus;

        for(int i = 0; i<1500; i++)  
        {
            for(int j = 0; j<3; j++)  
            {
                carrierStatus = randomBool();  
                r1 = Math.random();  
                s1 = new Station((int) r1,carrierStatus);

                carrierStatus = randomBool();
                r1=Math.random();  
                s2=new Station((int) r1,carrierStatus);

                carrierStatus = randomBool();
                r1=Math.random();  
                s3=new Station((int) r1,carrierStatus);

                System.out.println("sending message from s1 to s2");



                 if(s1.isCarrierStatus() && s2.isCarrierStatus())
                {
                    System.out.println("Collision occur! Transmiss stop!");
                    r4 = Math.random();
                    try
                    {
                        Thread.sleep((long) r4);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }

                else if(s1.isCarrierStatus()==false&&s2.isCarrierStatus()==false)
                {
                    r2=Math.random();
                    s2.setMessage((int) r2);
                    System.out.println("Data has been transmited successfully!");
                }

                else
                {
                    System.out.println("s1 or s2 is looking busy!");
                    r3=Math.random();
                    try
                    {
                        Thread.sleep((long) r3);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                System.out.println("sending message from s2 to s3");



                if(s2.isCarrierStatus()&&s3.isCarrierStatus())
                {
                    System.out.println("Collision occur! Transmiss stop!");
                    r4 = Math.random();
                    try
                    {
                        Thread.sleep((long) r4);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
                else if(s2.isCarrierStatus()==false&&s3.isCarrierStatus()==false)

                {
                    r2=Math.random();
                    s3.setMessage((int) r2);
                    System.out.println("Data has been transmited successfully!");
                }
                else
                {
                    System.out.println("s2 or s3 is looking busy!");
                    r3=Math.random();
                    try
                    {
                        Thread.sleep((long) r3);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }

                System.out.println("sending message from s3 to s1");



                if(s3.isCarrierStatus()&&s1.isCarrierStatus())
                {
                    System.out.println("Collision occur! Transmiss stop!");
                    r4=Math.random();
                    try
                    {
                        Thread.sleep((long) r4);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }

                else if(s3.isCarrierStatus()==false&&s1.isCarrierStatus()==false)
                {
                    r2=Math.random();
                    s1.setMessage((int) r2);
                    System.out.println("Data has been transmited successfully!");
                }
                else
                {
                    System.out.println("s3 or s1 is looking busy!");
                    r3=Math.random();
                    try
                    {
                        Thread.sleep((long) r3);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}



//Station Class

public class Station
{
    private int message;
    private boolean carrierStatus;
    private int reveivedMessage;
    public Station(int message, boolean carrierStatus)
    {
        this.message = message;
        this.carrierStatus = carrierStatus;
    }

    public int getMessage()
    {
        return message;
    }

    public void setMessage(int message)
    {
        this.message = message;
    }

    public boolean isCarrierStatus()
    {
        return carrierStatus;
    }

    public void setCarrierStatus(boolean carrierStatus)
    {
        this.carrierStatus = carrierStatus;
    }

    public int getReveivedMessage()
    {
        return reveivedMessage;
    }

    public void setReveivedMessage(int reveivedMessage)
    {
        this.reveivedMessage = reveivedMessage;
    }
}

Simulation +main(args: Stringl): void +check station_status(): void tupdate_station_status(): void +update_carrier_status(): void Class Diagram Station Carrier -station status: boolean probability double -R2_start transmission: ticks -R3_delay1: ticks -R4_delay2: ticks Set/get parameters R5_carrier length: met +set/get parameters

Expert Answer

 

class ThreadTest1

{

public static void main(String args[])

{

MyThread thread1 = new MyThread(“thread1: “);

MyThread thread2 = new MyThread(“thread2: “);

thread1.start();

thread2.start();

boolean thread1IsAlive = true;

boolean thread2IsAlive = true;

do {

if (thread1IsAlive && !thread1.isAlive()) {

thread1IsAlive = false;

System.out.println(“Thread 1 is dead.”);

}

if (thread2IsAlive && !thread2.isAlive()) {

thread2IsAlive = false;

System.out.println(“Thread 2 is dead.”);

}

} while(thread1IsAlive || thread2IsAlive);

}

}

class MyThread extends Thread

{

static String message[] =

{ “Java”, “is”, “hot,”, “aromatic,”, “and”, “invigorating.”};

public MyThread(String id)

{

super(id);

}

public void run()

{

String name = getName();

for (int i=0;i<message.length;++i) {

randomWait();

System.out.println(name + message[i]);

}

}

void randomWait()

{

try {

sleep((long)(3000*Math.random()));

} catch (InterruptedException x) {

System.out.println(“Interrupted!”);

}

}

}

The source code of the ThreadSynchronization program.

class ThreadSynchronization

{

public static void main(String args[])

{

MyThread thread1 = new MyThread(“thread1: “);

MyThread thread2 = new MyThread(“thread2: “);

thread1.start();

thread2.start();

boolean thread1IsAlive = true;

boolean thread2IsAlive = true;

do {

if (thread1IsAlive && !thread1.isAlive()) {

thread1IsAlive = false;

System.out.println(“Thread 1 is dead.”);

}

if (thread2IsAlive && !thread2.isAlive()) {

thread2IsAlive = false;

System.out.println(“Thread 2 is dead.”);

}

} while(thread1IsAlive || thread2IsAlive);

}

}

class MyThread extends Thread

{

static String message[] =

{ “Java”, “is”, “hot,”, “aromatic,”, “and”, “invigorating.”};

public MyThread(String id)

{

super(id);

}

public void run()

{

SynchronizedOutput.displayList(getName(),message);

}

void randomWait()

{

try {

sleep((long)(3000*Math.random()));

} catch (InterruptedException x) {

System.out.println(“Interrupted!”);

}

}

}

class SynchronizedOutput

{

public static synchronized void displayList(String name,String list[])

{

for(int i=0;i<list.length;++i) {

MyThread t = (MyThread) Thread.currentThread();

t.randomWait();

System.out.println(name+list[i]);

}

}

}

import java.util.Vector;

class Producer extends Thread {

static final int MAXQUEUE = 5;

private Vector messages = new Vector();

public void run() {

try {

while ( true ) {

putMessage();

sleep( 1000 );

}

}

catch( InterruptedException e ) { }

}

private synchronized void putMessage()

throws InterruptedException

{

 

while ( messages.size() == MAXQUEUE )

wait();

messages.addElement( new java.util.Date().toString() );

notify();

}

// Called by Consumer

public synchronized String getMessage()

throws InterruptedException

{

notify();

while ( messages.size() == 0 )

wait();

String message = (String)messages.firstElement();

messages.removeElement( message );

return message;

}

}

class Consumer extends Thread {

Producer producer;

 

Consumer(Producer p) {

producer = p;

}

public void run() {

try {

while ( true ) {

String message = producer.getMessage();

System.out.println(“Got message: ” + message);

sleep( 2000 );

}

}

catch( InterruptedException e ) { }

}

public static void main(String args[]) {

Producer producer = new Producer();

producer.start();

new Consumer( producer ).start();

}

}

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.