Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a;
- import java.util.concurrent.Semaphore;
- class BoundedBuffer{
- private Semaphore mutex, empty, full;
- private int size=0, occupied, ins, outs,buffer_size;
- private int[] Buffer;
- private int max_rand = 1000;
- public BoundedBuffer(int length){
- this.buffer_size = length;
- Buffer = new int[buffer_size];
- occupied = ins = outs = 0;
- full = new Semaphore(0);
- empty = new Semaphore(10);
- mutex = new Semaphore(1);
- }
- public void printstatus() {
- System.out.println("Items inserted : " + ins);
- System.out.println("Items removed : " + outs);
- System.out.println("Spaces occupied : " + occupied);
- System.out.println("Delta : " + (ins - outs - occupied));
- }
- public void insertItem(int Item) throws InterruptedException{
- while(true){
- empty.acquire();
- mutex.acquire();
- Buffer[size] = Item;
- size++;
- occupied++;
- ins++;
- mutex.release();
- full.release();
- }
- }
- public int removeItem() throws InterruptedException{
- while(true){
- int Item;
- full.acquire();
- mutex.acquire();
- size--;
- Item = Buffer[size];
- occupied--;
- outs++;
- mutex.release();
- empty.release();
- return Item;
- }
- }
- }
- class Producer extends Thread {
- public BoundedBuffer workspace;
- public int currentItem;
- public Producer(BoundedBuffer workspace) {
- this.workspace = workspace;
- }
- public void run() {
- try {
- while(true) {
- currentItem = Math.round(Math.round(Math.random() * 100));
- workspace.insertItem(currentItem);
- sleep((int)(Math.random() * 30));
- }
- } catch (InterruptedException e) {
- System.out.println("Producer thread exiting now");
- }
- }
- }
- class Consumer extends Thread {
- public BoundedBuffer workspace;
- public int currentItem;
- public Consumer(BoundedBuffer workspace) {
- this.workspace = workspace;
- }
- public void run() {
- try {
- while(true) {
- currentItem = workspace.removeItem();
- sleep((int)(Math.random() * 30));
- }
- } catch (InterruptedException e) {
- System.out.println("Consumer thread exiting now");
- }
- }
- }
- class Watcher extends Thread {
- public BoundedBuffer workspace;
- public Watcher(BoundedBuffer workspace) {
- this.workspace = workspace;
- }
- public void run() {
- try {
- while(true) {
- workspace.printstatus();
- sleep(2000);
- }
- } catch (InterruptedException e) {
- System.out.println("Watcher thread exiting now");
- }
- }
- }
- // Main
- class Uildver {
- public static void main(String[] args) throws InterruptedException {
- int buff_size = 50;
- if(args.length>0) {
- try {
- buff_size = Integer.parseInt(args[0]);
- } catch (NumberFormatException e) {
- System.err.println("Argument must be an integer.");
- }
- }
- BoundedBuffer space = new BoundedBuffer(buff_size);
- // create our threads
- Producer pthread = new Producer(space);
- Consumer cthread = new Consumer(space);
- Watcher wthread = new Watcher(space);
- // start our threads
- wthread.start();
- pthread.start();
- cthread.start();
- Thread.sleep(20*1000);
- Consumer cthread1 = new Consumer(space);
- cthread1.start();
- Thread.sleep(20*1000);
- Producer pthread1 = new Producer(space);
- Producer pthread2 = new Producer(space);
- pthread1.start();
- pthread2.start();
- Thread.sleep(20*1000);
- Consumer cthread2 = new Consumer(space);
- cthread2.start();
- // wait 2 minutes
- Thread.sleep(1 * 60 * 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement