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

Java Synchronization In telecommunications, a transceiver is a device comprising

ID: 3581720 • Letter: J

Question

Java Synchronization
In telecommunications, a transceiver is a device comprising both a transmitter and a receiver which are combined and share a single buffer housing. Specifically, our transceiver has one transmitter, m receivers, and shares a circular bounded buffer of size n bytes (a slot accommodates 1 byte) in which a byte sent by the transmitter should be received by all m receivers. Hence, if buffer is empty, the receivers will wait until data is available. Meanwhile, the transmitter can send data if there are any empty buffer slots.


Using Java “ReentrantLock” synchronization, write a program to synchronize the sending and receiving operations of the transmitter and receivers respectively. Your program should transmit 1000 bytes and the output should display all transceiver operations. Note: Buffer size n and number of receivers m should be initialized to the values entered by the user at the program runtime.

Explanation / Answer

PROGRAM CODE:

Transmitter.java

package samplepro;

import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class Transmitter {
  
   String message;
   int bufferSize;
  
   public void setMessage(String message)
   {
       this.message = message;
   }
  
   public void setSize(int size)
   {
       bufferSize = size;
   }
   public byte[] transmitMessage()
   {
       ByteArrayOutputStream bOutput = new ByteArrayOutputStream(1000);
       while(bOutput.size()!=bufferSize)
       {
           try {
               bOutput.write(message.getBytes());
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }
       byte b [] = bOutput.toByteArray();
       return b;
   }
  
}

Receiver.java

package samplepro;

import java.io.ByteArrayInputStream;

public class Receiver {
   static int receiverID;
  
   public Receiver() {
       ++receiverID;
      
   }
   public boolean receiveMessage( byte[] b)
   {
       System.out.println("Receiver id: "+ receiverID);
       ByteArrayInputStream bInput = new ByteArrayInputStream(b);
       int c;
while(( c = bInput.read())!= -1) {
System.out.println(Character.toUpperCase((char)c));
}
   bInput.reset();
       return true;
   }
}

TransReceiveDriver.java

package samplepro;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;

public class TransReceiveDriver {
  
   public static void main(String args[])
   {
       final Transmitter transmitter = new Transmitter();
       final ArrayList<Receiver> listOfReceivers;
       final ReentrantLock lock = new ReentrantLock();
       int bufferSize;
       final int numOfReceivers;
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter the buffer size: ");
       bufferSize = scanner.nextInt();
       System.out.println("Enter the number of receivers: ");
       numOfReceivers = scanner.nextInt();
       listOfReceivers = new ArrayList<Receiver>(numOfReceivers);
       transmitter.setSize(bufferSize);
       transmitter.setMessage("hello");
      
       Thread t1 = new Thread(){

@Override
public void run() {
   int i=0;
while (i <numOfReceivers) {
try {
   Receiver receiver = new Receiver();
   lock.lock();
           if(receiver.receiveMessage(transmitter.transmitMessage()));
               lock.unlock();
           listOfReceivers.add(receiver);
           i++;
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace(); }
}
}
};
t1.run();
   }
}

OUTPUT:

Enter the buffer size:
100
Enter the number of receivers:
2
Receiver id: 1
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
Receiver id: 2
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O
H
E
L
L
O

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