Question
Describe briefly what happens in Msynch (written in Java) when the first thread calls acquire ( ) the first time and each time another thread
- Describe briefly what happens in Msynch (written in Java) when the first thread calls acquire ( ) the first time and each time another thread calls replyReceived ( ).
- Find 3 synchronization-related errors in Msynch1. (This can be done through line-by-line comparison.) Explain why each error will cause Msynch1 not to work.
class Msynch
{
int replies;
int currentState = 1;
synchronized void acquire ( )
{ // Called by thread wanting access to a critical section
while (currentState != 1) wait ( );
replies = 0; currentState = 2;
//
// (Here, 5 messages are sent)
//
while (replies < 5) wait ( ); // Await 5 replies
currentState = 3;
}
synchronized void replyReceived ( )
{ // Called by communication thread when reply is received
replies++;
notifyAll ( );
}
synchronized void release ( )
{ // Called by a thread releasing the critical section
currentState = 1;
notifyAll ( );
}
}
class Msynch1
{
int replies;
int currentState = 1;
synchronized void acquire ( )
{ // Called by thread wanting access to a critical section
while (currentState != 1) yield ( );
replies = 0;
currentState = 2;
//
// (Here, 5 messages are sent)
//
if (replies < 5) wait ( ); // Await 5 replies
currentState = 3;
}
synchronized void replyReceived ( )
{ // Called by communication thread when reply is received
replies++;
}
synchronized void release ( )
{ // Called by a thread releasing the critical section
currentState = 1;
notifyAll ( );
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
In Msynch when the first thread calls the acquire method for the first time it enters a synchronized block It checks the value of the currentState var...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started