Describe briefly what happens in Msynch (written in Java) when the first thread calls acquire ( )
Question:
- 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 ( );
}
}