Question: Implement the stop(), send(), retrieve(), and run() methods. Pay special attention to the synchronization to avoid race conditions. import java.util.; class PostBox implements Runnable {

 Implement the stop(), send(), retrieve(), and run() methods. Pay special attention

Implement the stop(), send(), retrieve(), and run() methods. Pay special attention to the synchronization to avoid race conditions.

import java.util.; class PostBox implements Runnable { private final int MAX_SIZE; class Message String sender; String recipient; String asg; Message(String sender, String recipient, String msg) { this. sender = sender; this.recipient - recipient; this.msg = msg } private final LinkedList my Messages; private String myId; private volatile boolean stop = false; public PostBox(String myId, int max_size) { messages = new LinkedList(); this.myid - myId; this.myMessages = new LinkedList(); this. MAX SIZE = max_size; new Thread(this).start(); } public PostBox(String myId, int max_size, PostBox p) { this.myid = myId; this messages - p.messages; this.MAX_SIZE = max_size; this.myMessages = new LinkedList(); new Thread(this).start(); } public String getId() { return myId; } public void stop() { // make it so that this Runnable will stop when it next wakes } public void send (String recipient, String msg) { // add a message to the shared message queue } public List retrieve() { // return the contents of myMessages // and enpty myMessages } public void run() { // loop while not stopped 1. approximately once every second move all messages // addressed to this post box from the shared message queue to the private myMessages queue 2. also approximately once every second, if the private or shared message queue has more than MAX_SIZE messages, delete oldest messages so that the size of myMessages and messages is at most MAX SIZE } )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Databases Questions!