Answer to Question #241481 in Java | JSP | JSF for Zia

Question #241481

write a multi-threaded Java program that 


(i) Has a single Producer class object which is a thread and a single Consumer class which is also a thread. 


(ii) Create a class Packet. This class should contain a simple string. 


(iii) Create a class Buffer that internally uses some data structure to hold the incoming packet objects – it should have methods insertPkt and remove Pkt and should have methods to tell the user how many packets are in the Buffer at any time e.g. size.


(iv) Make the Producer object at random time intervals create Packet objects and place them in the Buffer object.


(v) OBJECTIVE: Use a mechanism from the books Concurrency chapter to synchronize the producer thread pushing packets into the Buffer and the consumer thread removing them such that the Buffer size always remains between 50-70 packets. 


(vi) Show through any means how your objective is met by running the Producer and Consumer object.


1
Expert's answer
2021-09-23T17:44:35-0400
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Buffer buffer = new Buffer();
        Producer producer = new Producer(buffer);
        producer.start();
    }
}

class Producer extends Thread {
    private final Buffer buffer;
    public Producer(Buffer buffer) {
        this.buffer = buffer;
    }
    @Override
    public void run() {
        Random random = new Random();
        while (true) {
            buffer.insertPacket(new Packet());
            try {
                Thread.sleep(random.nextLong());
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}

class Consumer extends Thread {
    private final Buffer buffer;
    public Consumer(Buffer buffer) {
        this.buffer = buffer;
    }
    @Override
    public void run() {
        Random random = new Random();
        while (true) {
            buffer.removePacket(random.nextInt());
            try {
                Thread.sleep(random.nextLong());
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}

class Packet {
    private String string;
    public String getString() {
        return string;
    }
    public void setString(String string) {
        this.string = string;
    }
}


class Buffer {
    private final List<Packet> list;
    public Buffer() {
        this.list = new ArrayList<Packet>();
    }
    public synchronized void insertPacket(Packet packet) {
        this.list.add(packet);
    }
    public synchronized void removePacket(int i) {
        this.list.remove(i % this.list.size());
    }
    public int size() {
        return list.size();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS