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

Reverse engineer a couple of programs and trace through it to understand what’s

ID: 3908687 • Letter: R

Question

Reverse engineer a couple of programs and trace through it to understand what’s going on. Your end product will be an algorithm that describes how the program functions. Please help me write out an algorithm in high-level pseudocode that illustrates the program’s tasks, kindly comment on each line so that I can have my algorithm laid out.

Here's the Java programs:

// Buffer.java
// Buffer interface specifies methods called by Producer and Consumer.
public interface Buffer
{
// place int value into Buffer
public void blockingPut(int value) throws InterruptedException;

// obtain int value from Buffer
public int blockingGet() throws InterruptedException;
} // end interface Buffer

-------------------------------------------------------------

// Consumer.java
// Consumer with a run method that loops, reading 10 values from buffer.
import java.security.SecureRandom;

public class Consumer implements Runnable
{
private static final SecureRandom generator = new SecureRandom();
private final Buffer sharedLocation; // reference to shared object

// constructor
public Consumer(Buffer sharedLocation)
{
this.sharedLocation = sharedLocation;
}

// read sharedLocation's value 10 times and sum the values
public void run()
{
int sum = 0;

for (int count = 1; count <= 10; count++)
{
// sleep 0 to 3 seconds, read value from buffer and add to sum
try
{
Thread.sleep(generator.nextInt(3000));
sum += sharedLocation.blockingGet();
}
catch (InterruptedException exception)
{
Thread.currentThread().interrupt();
}
}

System.out.printf("%n%s %d%n%s%n",
"Consumer read values totaling", sum, "Terminating Consumer");
}
} // end class Consumer

-----------------------------------------------------------------

// Producer.java
// Producer with a run method that inserts the values 1 to 10 in buffer.
import java.security.SecureRandom;

public class Producer implements Runnable
{
private static final SecureRandom generator = new SecureRandom();
private final Buffer sharedLocation; // reference to shared object

// constructor
public Producer(Buffer sharedLocation)
{
this.sharedLocation = sharedLocation;
}

// store values from 1 to 10 in sharedLocation
public void run()
{
int sum = 0;

for (int count = 1; count <= 10; count++)
{
try // sleep 0 to 3 seconds, then place value in Buffer
{
Thread.sleep(generator.nextInt(3000)); // random sleep
sharedLocation.blockingPut(count); // set value in buffer
sum += count; // increment sum of values
}
catch (InterruptedException exception)
{
Thread.currentThread().interrupt();
}
}

System.out.printf(
"Producer done producing%nTerminating Producer%n");
}
} // end class Producer

------------------------------------------------------------------------

// SharedBufferTest2.java
// Two threads correctly manipulating a synchronized buffer.
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SharedBufferTest2
{
public static void main(String[] args) throws InterruptedException
{
// create a CachedThreadPool
ExecutorService executorService = Executors.newCachedThreadPool();

// create SynchronizedBuffer to store ints
Buffer sharedLocation = new SynchronizedBuffer();

System.out.printf("%-40s%s %s%n%-40s%s%n%n", "Operation",
"Buffer", "Occupied", "---------", "------ --------");

// execute the Producer and Consumer tasks
executorService.execute(new Producer(sharedLocation));
executorService.execute(new Consumer(sharedLocation));

executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
}
} // end class SharedBufferTest2

-------------------------------------------------------------------

// SynchronizedBuffer.java
// Synchronizing access to shared mutable data using Object
// methods wait and notifyAll.
public class SynchronizedBuffer implements Buffer
{
private int buffer = -1; // shared by producer and consumer threads
private boolean occupied = false;

// place value into buffer
public synchronized void blockingPut(int value)
throws InterruptedException
{
// while there are no empty locations, place thread in waiting state
while (occupied)
{
// output thread information and buffer information, then wait
System.out.println("Producer tries to write."); // for demo only
displayState("Buffer full. Producer waits."); // for demo only
wait();
}
  
buffer = value; // set new buffer value
  
// indicate producer cannot store another value
// until consumer retrieves current buffer value
occupied = true;
  
displayState("Producer writes " + buffer); // for demo only
  
notifyAll(); // tell waiting thread(s) to enter runnable state
} // end method blockingPut; releases lock on SynchronizedBuffer
  
// return value from buffer
public synchronized int blockingGet() throws InterruptedException
{
// while no data to read, place thread in waiting state
while (!occupied)
{
// output thread information and buffer information, then wait
System.out.println("Consumer tries to read."); // for demo only
displayState("Buffer empty. Consumer waits."); // for demo only
wait();
}

// indicate that producer can store another value
// because consumer just retrieved buffer value
occupied = false;

displayState("Consumer reads " + buffer); // for demo only
  
notifyAll(); // tell waiting thread(s) to enter runnable state

return buffer;
} // end method blockingGet; releases lock on SynchronizedBuffer
  
// display current operation and buffer state; for demo only
private synchronized void displayState(String operation)
{
System.out.printf("%-40s%d %b%n%n", operation, buffer,
occupied);
}
} // end class SynchronizedBuffer

Explanation / Answer

The pseudo code for the files are as follows:

1. Buffer.java

// Buffer interface specifies methods called by Producer and Consumer

Create an Interface named Buffer and Declare two methods

// obtain int value from Buffer

one to get the count by consumer

// place int value into Buffer

one to put the value by the producer

-------------------------------------------------------------------------------------------------------------------------------------------------------

2. Consumer.java
// Consumer with a run method that loops, reading 10 values from buffer.

Initialise generator variable with a random number

Declare an instance of Buffer interface.

define a parameterized constructor having the Buffer instance and assign it to the object

set the sum to 0

set count to 1

While count is less than or equal to ten

print sum variable

-------------------------------------------------------------------------------------------------------------------------------------------------------

3. producer.java
// Producer with a run method that inserts the values 1 to 10 in buffer.

Initialise generator variable with a random number

Declare an instance of Buffer interface.

define a parameterized constructor having the Buffer instance and assign it to the object

set the sum to 0

set count to 1

While count is less than or equal to ten

print sum variable

-----------------------------------------------------------------------------------------------------------------------------------------------------

4. SharedBufferTest2.java

// Two threads correctly manipulating a synchronized buffer.

create a CachedThreadPool with Executor Service

create SynchronizedBuffer to store int values

// execute the Producer and Consumer tasks

start the Producer task first

start the consumer task next

wait for the termination of both the threads

END

--------------------------------------------------------------------------------------------------------------------------------------------------

5.SynchronizedBuffer.java

// Synchronizing access to shared mutable data using Object

Set buffer to -1

set boolean variable occupied to false

// place value into buffer

Define the overriden synchronised BlockingPut(count) method of Buffer interface

While there are no empty locations

Set new value to buffer object

set boolean variable occupied to false indicating producer cannot store more values

displayState("Producer writes " + buffer);

Notify all waiting threads

Define the overriden synchronised BlockingGet() method of Buffer interface

While there is no data to read

Set new value to buffer object

set boolean variable occupied to false indicating consumer cannot read more values

displayState("Consumer Reads" + buffer);

Notify all waiting threads

return buffer

END

-----------------------------------------------------------------------------------------------------------------------------------------

The code itself is self-explanatory..

so if u have any problem u can relate the pseudo code with the comments provided in the code.

Feel free to comment if you have any queries.

Cheers...!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote