Modify the following code using multi-threading Java implementation of the system.
At the minimum:
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. //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();
}
}