Modify the following code using multi-threading Java implementation of the syste
ID: 3867518 • Letter: M
Question
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.)
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 parametersExplanation / 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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.