From ba9469b326deef641be936b55eda72ac684a5fbd Mon Sep 17 00:00:00 2001 From: nagesh srinivas Date: Mon, 16 Mar 2020 22:31:07 +0530 Subject: [PATCH 1/3] Create threadpool.java --- threadpool.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 threadpool.java diff --git a/threadpool.java b/threadpool.java new file mode 100644 index 0000000..61e6fc3 --- /dev/null +++ b/threadpool.java @@ -0,0 +1,30 @@ +public class Threadpool extends Thread { + + private BlockingQueue taskQueue = null; + private boolean isStopped = false; + + public Threadpool(BlockingQueue queue){ + taskQueue = queue; + } + + public void run(){ + while(!isStopped()){ + try{ + Runnable runnable = (Runnable) taskQueue.dequeue(); + runnable.run(); + } catch(Exception e){ + //log or otherwise report exception, + //but keep pool thread alive. + } + } + } + + public synchronized void doStop(){ + isStopped = true; + this.interrupt(); //break pool thread out of dequeue() call. + } + + public synchronized boolean isStopped(){ + return isStopped; + } +} From 7f00069abed631243d25a57c0d8a6393b1a60ad9 Mon Sep 17 00:00:00 2001 From: nagesh srinivas Date: Mon, 16 Mar 2020 23:03:34 +0530 Subject: [PATCH 2/3] Create BlockingQueue.java --- BlockingQueue.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 BlockingQueue.java 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); + } + +} From 1258544043fa5182a86b9b105b7c2709b36a1acd Mon Sep 17 00:00:00 2001 From: nagesh srinivas Date: Mon, 16 Mar 2020 23:40:30 +0530 Subject: [PATCH 3/3] Create executor --- executor | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 executor 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