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

1 Expert Approved Answer
Step: 1 Unlock

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... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!