diff --git a/BlockingQueue.java b/BlockingQueue.java new file mode 100644 index 0000000..73483c1 --- /dev/null +++ b/BlockingQueue.java @@ -0,0 +1,35 @@ +public class BlockingQueue { + + private List queue = new LinkedList(); + private int limit = 10; + + public BlockingQueue(int limit){ + this.limit = limit; + } + + + public synchronized void enqueue(Object item) + throws InterruptedException { + while(this.queue.size() == this.limit) { + wait(); + } + this.queue.add(item); + if(this.queue.size() == 1) { + notifyAll(); + } + } + + + public synchronized Object dequeue() + throws InterruptedException{ + while(this.queue.size() == 0){ + wait(); + } + if(this.queue.size() == this.limit){ + notifyAll(); + } + + return this.queue.remove(0); + } + +} diff --git a/executor b/executor new file mode 100644 index 0000000..4fdbce4 --- /dev/null +++ b/executor @@ -0,0 +1,31 @@ +public class Executor { + + private BlockingQueue taskQueue = null; + private List threads = new ArrayList(); + private boolean isStopped = false; + + public Executor(int noOfThreads){ + taskQueue = new BlockingQueue(noOfThreads); + + for(int i=0; i