Java Threads Implementation The Java code in Chapter 6 Section 6.6.2 (Synchroniz
ID: 3599520 • Letter: J
Question
Java Threads Implementation The Java code in Chapter 6 Section 6.6.2 (Synchronization with Semaphores in Java), is only a high-level sketch with some Java code and is not complete. For more details on threads, refer to Chapter 6 and Appendix B in the textbook. You are required to complete the program of a program that implements the solution to the Producer-Consumer problem. Implement the shared buffer as an array of type char. The Producer and Consumer objects must each display when they have accessed the shared buffer. You may simplify the problem by defining a buffer with a single text character.
-Provide a well-define structure of the Java code to submit.
-Include appropriate comments to clarify your implementation.
-Indicate the code that you added and show the results.
-You may use (but need to include the reference).
-Show the appropriate computer output compilation and running of the program.
-Write a report with a good and appropriate documentation, source code, results, etc. Follow the guidelines for writing the report.
The Java code in Chapter 6 Section 6.6.2 (Synchronization with Semaphores in Java):
class Semaphore {
private int sem;
//
public synchronized void wait() {
while ( sem <= 0) {
try {
swait ();
} catch (Exception e) { System.exit(O);};
} // end while
sem--; // decrease value of sem
}// end swait
//
public synchronized void signal() {
sem++;
notify();
} // end signal
//
// constructor
public Semaphore ( int intval) {
sem = intval; II initialize attribute sem
} // end class Semaphore
public class Producer extends Thread {
private Semaphore mutex;
private Semaphore full;
private Semaphore empty;
public Producer (Semaphore m, Semaphore f, Semaphore e) {
mutex = m;
full = f;
empty = e;
} // end constructor
public void run() {
while (true) {
[ produce an item ]
empty.wait();
mutex.waitO;
[ deposit an item into an
mutex. signal() ;
// are there any empty slots?
// acquire exclusive access
empty slot of the buff er ]
// release mutual exclusion
// increment full slots
}
}
full. signal() ;
} // end while loop
// end run
// end class Producer
public class Consumer extends Thread {
private Semaphore mutex;
private Semaphore full;
private Semaphore empty;
public Consumer (Semaphore m, Semaphore f, Semaphore e) {
mutex = m;
full = f;
empty = e;
} // end constructor
//
public run() {
while (true) {
full.wait(); // are there any full slots?
mutex.wait(); // acquire exclusive access
[ remove an item from a full slot of the buffer ]
mutex. signal() ; // release mutual exclusion
empty. signal(); // increment empty slots
[ Consume data item ]
} // end while loop
} // end while
} // end run
} // end class Consumer
public class ProdconSync {
static final int N = 100; // number of slots in buffer
public static void main (String args[]) {
Semaphore mutex = new Semaphore (1);
Semaphore full= new Semaphore (0);
Semaphore empty= new Semaphore (N);
Producer prod= new Producer (mutex, full, empty);
Consumer cons= new Consumer (mutex, full, empty);
prod. start() ;
cons.start();
} // end main
} // end class ProdconSync
Explanation / Answer
package pocker;
import java.util.Date;
class Semaphore {
private int sem;
//
public synchronized void swait() {
while ( sem <= 0) {
try {
wait (500);
} catch (Exception e) { System.exit(0);};
}// II end while
sem--; // II decrease value of sem
}// end swait
//
public synchronized void signal() {
sem++;
notify();
} // end signal
//
// constructor
public Semaphore ( int intval) {
sem = intval; // initialize attribute sem
} // end class Semaphore
}
public class Solution{
public static void main(String args[]) {
//instantiate (create) buffer shared by Producer & Consumer
Buffer sharedBuffer = new BoundedBuffer();
// create the producer and consumer threads
Thread producerThread = new Thread(new Producer(sharedBuffer));
Thread consumerThread = new Thread(new Consumer(sharedBuffer));
//start() method allocates memory for a new thread in the JVM,
//and calls the run() method
producerThread.start();
consumerThread.start();
}
}
public abstract void insert(Object item);
/**
* remove an item from the Buffer.
* Note this may be either a blocking
* or non-blocking operation.
*/
public abstract Object remove();
class BoundedBuffer implements Buffer{
private static final int BUFFER_SIZE = 500; //max size of buffer array
private int count; //number of items currently in the buffer
private int in; // points to the next free position in the buffer
private int out; // points to the first filled position in the buffer
private Object[] buffer; //array of Objects
private Semaphore mutex; //provides limited access to the buffer (mutual exclusion)
private Semaphore empty; //keep track of the number of empty elements in the array
private Semaphore full; //keep track of the number of filled elements in the array
public BoundedBuffer(){
// buffer is initially empty
count = 0;
in = 0;
out = 0;
buffer = new Object[BUFFER_SIZE];
mutex = new Semaphore(1); //1 for mutual exclusion
empty = new Semaphore(BUFFER_SIZE); //array begins with all empty elements
full = new Semaphore(0); //array begins with no elements
}
// producer calls this method
public void insert(Object item) {
empty.swait(); //keep track of number of empty elements (value--)
//This provides synchronization for the producer,
//because this makes the producer stop running when buffer is full
mutex.swait(); //mutual exclusion
// add an item to the buffer
++count;
buffer[in] = item;
//modulus (%) is the remainder of a division
//for example, 0%3=0, 1%3=1, 2%3=2, 3%3=0, 4%3=1, 5%3=2
in = (in + 1) % BUFFER_SIZE;
//buffer information feedback
if (count == BUFFER_SIZE){
System.out.println("BUFFER FULL "
+ "Producer inserted "" + item
+ "" count=" + count + ", "
+ "in=" + in + ", out=" + out);
}
else{
System.out.println("Producer inserted "" + item
+ "" count=" + count + ", "
+ "in=" + in + ", out=" + out);
}
mutex.signal(); //mutual exclusion
full.signal(); //keep track of number of elements (value++)
//If buffer was empty, then this wakes up the Consumer
}
// consumer calls this method
public Object remove() {
Object item=null;
/*
while (count == 0){
//if nothing in the buffer, then do nothing
//the buffer array cannot be used (because empty)
}
*/
full.swait(); //keep track of number of elements (value--)
mutex.swait(); //mutual exclusion
// remove an item from the buffer
--count;
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
//buffer information feedback
if (count == 0){
System.out.println("BUFFER EMPTY "
+ "Consumer removed "" + item
+ "" count=" + count + ", "
+ "in=" + in + ", out=" + out);
}
else{
System.out.println("Consumer removed "" + item
+ "" count=" + count + ", "
+ "in=" + in + ", out=" + out);
}
mutex.signal(); //mutual exclusion
empty.signal(); //keep track of number of empty elements (value++)
//if buffer was full, then this wakes up the Producer
return item;
}
}
class Producer extends Thread{
private Buffer buffer;
public Producer(Buffer b) {
buffer = b;
}
public void run(){
Date message;
while (true) {
System.out.println("Producer napping");
SleepUtilities.nap();
message = new Date();
System.out.println("Producer produced "" + message + """);
buffer.insert(message);
}
}
}
class Consumer extends Thread{
private Buffer buffer;
public Consumer(Buffer b) {
buffer = b;
}
public void run(){
Date message = null;
while (true){
System.out.println("Consumer napping");
SleepUtilities.nap();
System.out.println("Consumer wants to consume");
message = (Date)buffer.remove();
System.out.println("Consumer consumed "" + message + """);
}
}
}
class SleepUtilities{
private static final int NAP_TIME = 5; //max nap time in seconds
/**
* Nap between zero and NAP_TIME seconds.
*/
public static void nap() {
nap(NAP_TIME);
}
/**
* Nap between zero and duration seconds.
*/
public static void nap(int duration) {
int sleeptime = (int) (NAP_TIME * Math.random() );
System.out.println("Nap for " + sleeptime + " seconds");
try { Thread.sleep(sleeptime*1000); }
catch (InterruptedException e) {
System.out.println("ERROR in nap(): " + e);
}
}
}
/* output:-
Producer napping
Consumer napping
Nap for 4 seconds
Nap for 4 seconds
Consumer wants to consume
Producer produced "Sun Oct 22 22:08:20 IST 2017"
Producer inserted "Sun Oct 22 22:08:20 IST 2017" count=1, in=1, out=0
Producer napping
Nap for 0 seconds
BUFFER EMPTY Consumer removed "Sun Oct 22 22:08:20 IST 2017" count=0, in=1, out=1
Consumer consumed "Sun Oct 22 22:08:20 IST 2017"
Consumer napping
Nap for 2 seconds
Producer produced "Sun Oct 22 22:08:20 IST 2017"
Producer inserted "Sun Oct 22 22:08:20 IST 2017" count=1, in=2, out=1
Producer napping
Nap for 0 seconds
Producer produced "Sun Oct 22 22:08:20 IST 2017"
Producer inserted "Sun Oct 22 22:08:20 IST 2017" count=2, in=3, out=1
Producer napping
Nap for 4 seconds
Consumer wants to consume
Consumer removed "Sun Oct 22 22:08:20 IST 2017" count=1, in=3, out=2
Consumer consumed "Sun Oct 22 22:08:20 IST 2017"
Consumer napping
Nap for 3 seconds
Producer produced "Sun Oct 22 22:08:24 IST 2017"
Producer inserted "Sun Oct 22 22:08:24 IST 2017" count=2, in=4, out=2
Producer napping
Nap for 4 seconds
Consumer wants to consume
Consumer removed "Sun Oct 22 22:08:20 IST 2017" count=1, in=4, out=3
Consumer consumed "Sun Oct 22 22:08:20 IST 2017"
Consumer napping
Nap for 0 seconds
Consumer wants to consume
BUFFER EMPTY Consumer removed "Sun Oct 22 22:08:24 IST 2017" count=0, in=4, out=4
Consumer consumed "Sun Oct 22 22:08:24 IST 2017"
Consumer napping
Nap for 3 seconds
..........continue until you terminate the program.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.