Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need help changing this java code to UnSynchronized instead of synchronized .

ID: 3716955 • Letter: I

Question

i need help changing this java code to UnSynchronized instead of synchronized .

import java.text.DecimalFormat;
   
    // Java extension packages
   import javax.swing.*;
  
   public class HoldIntegerSynchronized {
     
      // array of shared locations
      private int sharedInt[] = { -1, -1, -1, -1, -1 };
  
      // variables to maintain buffer information
      private boolean writeable = true;
      private boolean readable = false;
      private int readLocation = 0, writeLocation = 0;
  
      // GUI component to display output
      private JTextArea outputArea;
  
      // initialize HoldIntegerSynchronized
      public HoldIntegerSynchronized( JTextArea output )
      {
         outputArea = output;
     }
      public synchronized void setSharedInt( int value )
      {
         while ( !writeable ) {
  
            // thread that called this method must wait
            try {
  
               // update Swing GUI component
               SwingUtilities.invokeLater( new UpdateThread(
                  outputArea, " WAITING TO PRODUCE " + value ) );
  
               wait();
            }
  
            // process InterrupteException while thread waiting
            catch ( InterruptedException exception ) {
               System.err.println( exception.toString() );
            }
         }
  
         // place value in writeLocation
         sharedInt[ writeLocation ] = value;
  
         // indicate that consumer can read a value
         readable = true;
  
         // update Swing GUI component
         SwingUtilities.invokeLater( new UpdateThread( outputArea,
            " Produced " + value + " into cell " +
            writeLocation ) );
    writeLocation = ( writeLocation + 1 ) % 5;
  
         // update Swing GUI component
         SwingUtilities.invokeLater( new UpdateThread( outputArea,
            " write " + writeLocation + " read " +
            readLocation ) );
  
         displayBuffer( outputArea, sharedInt );
  
         // test if buffer is full
         if ( writeLocation == readLocation ) {
            writeable = false;
  
            // update Swing GUI component
            SwingUtilities.invokeLater( new UpdateThread( outputArea,
               " BUFFER FULL" ) );
         }
  
         // tell a waiting thread to become ready
         notify();
    
      } // end method setSharedInt
  
      // synchronized method allows only one thread at a time to
      // invoke this method to get a value from a particular
      // HoldIntegerSynchronized object
      public synchronized int getSharedInt()
      {
         int value;
  
        while ( !readable ) {
try {

              // update Swing GUI component
              SwingUtilities.invokeLater( new UpdateThread(
                 outputArea, " WAITING TO CONSUME" ) );

              wait();
           }

           // process InterrupteException while thread waiting
           catch ( InterruptedException exception ) {
              System.err.println( exception.toString() );
           }
        }

        // indicate that producer can write a value
        writeable = true;

        // obtain value at current readLocation
        value = sharedInt[ readLocation ];

        // update Swing GUI component
        SwingUtilities.invokeLater( new UpdateThread( outputArea,
           " Consumed " + value + " from cell " +
           readLocation ) );

        // update read location for future read operation
        readLocation = ( readLocation + 1 ) % 5;

        // update Swing GUI component
        SwingUtilities.invokeLater( new UpdateThread( outputArea,
           " write " + writeLocation + " read " +
           readLocation ) );
        displayBuffer( outputArea, sharedInt );

        // test if buffer is empty
        if ( readLocation == writeLocation ) {
           readable = false;

           // update Swing GUI component
           SwingUtilities.invokeLater( new UpdateThread(
              outputArea, " BUFFER EMPTY" ) );
        }

        // tell a waiting thread to become ready
        notify();

        return value;

     } // end method getSharedInt

     // diplay contents of shared buffer
     public void displayBuffer( JTextArea outputArea,
        int buffer[] )
     {
        DecimalFormat formatNumber = new DecimalFormat( " #;-#" );
        StringBuffer outputBuffer = new StringBuffer();
  
        // place buffer elements in outputBuffer
        for ( int count = 0; count < buffer.length; count++ )
           outputBuffer.append(
              " " + formatNumber.format( buffer[ count ] ) );

        // update Swing GUI component
        SwingUtilities.invokeLater( new UpdateThread( outputArea,
           " buffer: " + outputBuffer ) );
     }
}

Explanation / Answer

To make it unsynchronized, remove all "synchronized" keyword of java from all methods and blocks. Below is the code .


import java.text.DecimalFormat;

// Java extension packages
import javax.swing.*;

public class HoldIntegerSynchronized {

// array of shared locations
private int sharedInt[] = {-1, -1, -1, -1, -1};

// variables to maintain buffer information
private boolean writeable = true;
private boolean readable = false;
private int readLocation = 0, writeLocation = 0;

// GUI component to display output
private JTextArea outputArea;

// initialize HoldIntegerSynchronized
public HoldIntegerSynchronized(JTextArea output) {
outputArea = output;
}

public void setSharedInt(int value) {
while (!writeable) {

// thread that called this method must wait
try {

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(
outputArea, " WAITING TO PRODUCE " + value));

wait();
} // process InterrupteException while thread waiting
catch (InterruptedException exception) {
System.err.println(exception.toString());
}
}

// place value in writeLocation
sharedInt[writeLocation] = value;

// indicate that consumer can read a value
readable = true;

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(outputArea,
" Produced " + value + " into cell "
+ writeLocation));
writeLocation = (writeLocation + 1) % 5;

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(outputArea,
" write " + writeLocation + " read "
+ readLocation));

displayBuffer(outputArea, sharedInt);

// test if buffer is full
if (writeLocation == readLocation) {
writeable = false;

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(outputArea,
" BUFFER FULL"));
}

// tell a waiting thread to become ready
notify();

} // end method setSharedInt

// synchronized method allows only one thread at a time to
// invoke this method to get a value from a particular
// HoldIntegerSynchronized object
public int getSharedInt() {
int value;

while (!readable) {
try {

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(
outputArea, " WAITING TO CONSUME"));

wait();
} // process InterrupteException while thread waiting
catch (InterruptedException exception) {
System.err.println(exception.toString());
}
}

// indicate that producer can write a value
writeable = true;

// obtain value at current readLocation
value = sharedInt[readLocation];

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(outputArea,
" Consumed " + value + " from cell "
+ readLocation));

// update read location for future read operation
readLocation = (readLocation + 1) % 5;

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(outputArea,
" write " + writeLocation + " read "
+ readLocation));
displayBuffer(outputArea, sharedInt);

// test if buffer is empty
if (readLocation == writeLocation) {
readable = false;

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(
outputArea, " BUFFER EMPTY"));
}

// tell a waiting thread to become ready
notify();

return value;

} // end method getSharedInt

// diplay contents of shared buffer
public void displayBuffer(JTextArea outputArea,
int buffer[]) {
DecimalFormat formatNumber = new DecimalFormat(" #;-#");
StringBuffer outputBuffer = new StringBuffer();

// place buffer elements in outputBuffer
for (int count = 0; count < buffer.length; count++) {
outputBuffer.append(
" " + formatNumber.format(buffer[count]));
}

// update Swing GUI component
SwingUtilities.invokeLater(new UpdateThread(outputArea,
" buffer: " + outputBuffer));
}
}