Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as text by berat ( 17 years ago )
import java.util.concurrent.*;

import java.util.concurrent.locks.*;

import java.util.*;



/**

 * Queue class represents the -only- customer queue inthe sustem.

 */

class Queue {

	int c,max;

    ReentrantLock lock, producerLock, consumerLock;
	long interval;
	long service;
	long waitingtime;
	

	public Queue (int max,long interval,long service) {

	    c=0;

	    this.max=max;

	    lock=new ReentrantLock();

	    producerLock=new ReentrantLock();

	    consumerLock=new ReentrantLock();
	    this.interval=interval;
	    this.service=service;
	   
	     

	}

	public int push() throws InterruptedException {

	    producerLock.lock();

	    for (;c>max;) { //wait until there is space in the queue

		//System.out.println("Waiting for queue to be emptied before pushing");

		Thread.currentThread().sleep(service);

	    } 

	    lock.lock();

	    setValue(getValue()+1);
	    long k = service - interval;
	    System.out.println("waitingtimeofotherProducerthread 
"+k);
            lock.unlock();
            

	    producerLock.unlock();

	    return c;

	}

	public int pop()  throws InterruptedException{

	    consumerLock.lock();

	    for (;c<=0;) { //wait until something is available

		//System.out.println("Waiting for queue to be filled before popping");

		Thread.currentThread().sleep(interval);

	    } 

	    lock.lock();

	    int temp=getValue();Thread.currentThread().sleep(1);

	    setValue(temp-1);

	    lock.unlock();

	    consumerLock.unlock();

	    return temp;

	}

	private int getValue() { return c;}

	private void setValue(int newval) { c=newval; }

}



class Producer implements Runnable{

	Queue q;

	long total;

	Producer(Queue q) {this.q=q;}

	public void run() {

		int x;

		for(;;) {

			try {

				x=q.push();

				//System.out.println(String.format("Thread no %d produced %d",Thread.currentThread().getId(),x));

				total+=1;

			} catch(InterruptedException e) {return;}

		}

	}

	public long getTotal() {return total;}

}



class Consumer  implements Runnable{

	Queue q;

	long total;

	public Consumer(Queue q) {this.q=q;}

	public void run() {

		int x;

		for(;;) {

			try {

				x=q.pop();

				//System.out.println(String.format("Thread no %d consumed %d",Thread.currentThread().getId(),x));

				total+=1;

			} catch(InterruptedException e) {return;}

		}

	}

	public long getTotal() {return total;}

}



public class ProducerConsumerHWSolution {

	public static void main(String[] args) {

		if (args.length<3) {

			System.out.println("No parameters. First parameter must be number of producers, second is number of consumers.");

			System.exit(1);

		}
		

		int numproducers=Integer.parseInt(args[0]);

		int numconsumers=Integer.parseInt(args[1]);
		long interval=Integer.parseInt(args[2]);
		long interval2 = interval * (long)(Math.random() * 100);
		long service=Integer.parseInt(args[2]);
		long service2 = service * (long)(Math.random() * 10000);

		Queue q=new Queue(1000,interval2,service);

		Producer[] producers=new Producer[numproducers];	

		Consumer[] consumers=new Consumer[numconsumers];

		Thread[] threadList=new Thread[numproducers+numconsumers];

		ThreadGroup tg=new ThreadGroup("My group");

		for (int i=0;i<numproducers;i++) {

			producers[i]=new Producer(q);	

			threadList[i]=new Thread(tg,producers[i]);

		}

		for (int i=0;i<numconsumers;i++) {

			consumers[i]=new Consumer(q);	

			threadList[i+numproducers]=new Thread(tg,consumers[i]);

		}

		//g.enumerate(threadList);

		for (int i=0;i<threadList.length;i++) {

			threadList[i].start();

		}

		for (;;) { 

			long totalProducers=0;

			long totalConsumers=0;

			for (int i=0;i<numproducers;i++) {

				totalProducers+=producers[i].getTotal();

			}

			for (int i=0;i<numconsumers;i++) {

				totalConsumers+=consumers[i].getTotal();

			}	

			System.out.println(String.format("TOTALS: produced %d, consumed %d, remains in queue %d",totalProducers,totalConsumers,q.c));

		}

	}

}

 

Revise this Paste

Your Name: Code Language: