Merge "Fixed for bug : 1171 - issue while creating subnet"
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLThreadPool.java
1 package org.opendaylight.controller.md.sal.dom.xsql;
2
3 import java.util.LinkedList;
4
5 public class XSQLThreadPool {
6     private LinkedList<Runnable> tasks = new LinkedList<Runnable>();
7     private int threadCount = 0;
8     private int maxThreadCount = 10;
9     private String threadPoolName = "Simple Thread Pool";
10     private int waitTimeForIdle = 10000;
11     private int maxQueueSize = -1;
12     public Object waitForSlotSync = new Object();
13
14     public XSQLThreadPool(int _maxThreadCount, String name,
15         int _waitTimeForIdle) {
16         this.maxThreadCount = _maxThreadCount;
17         this.threadPoolName = name;
18         this.waitTimeForIdle = _waitTimeForIdle;
19     }
20
21     public void addTask(Runnable r) {
22         synchronized (tasks) {
23             tasks.add(r);
24             tasks.notifyAll();
25             if (threadCount < maxThreadCount) {
26                 threadCount++;
27                 new WorkerThread(threadCount).start();
28             }
29         }
30     }
31
32     private class WorkerThread extends Thread {
33
34         private long lastTimeExecuted = System.currentTimeMillis();
35
36         public WorkerThread(int threadNumber) {
37             super(
38                 "Thread #" + threadNumber + " Of Threadpool " + threadPoolName);
39         }
40
41         public void run() {
42             Runnable runthis = null;
43             while (true) {
44                 runthis = null;
45                 if (maxQueueSize != -1) {
46                     synchronized (waitForSlotSync) {
47                         if (tasks.size() < maxQueueSize) {
48                             waitForSlotSync.notifyAll();
49                         }
50                     }
51                 }
52                 synchronized (tasks) {
53                     if (tasks.isEmpty()) {
54                         try {
55                             tasks.wait(2000);
56                         } catch (Exception err) {
57                         }
58                     }
59
60                     if (!tasks.isEmpty()) {
61                         runthis = tasks.removeFirst();
62                     }
63                 }
64                 if (runthis != null) {
65                     try {
66                         runthis.run();
67                     } catch (Exception err) {
68                         err.printStackTrace();
69                     }
70                     lastTimeExecuted = System.currentTimeMillis();
71                 }
72                 if (System.currentTimeMillis() - lastTimeExecuted
73                     > waitTimeForIdle) {
74                     break;
75                 }
76             }
77             synchronized (tasks) {
78                 threadCount--;
79             }
80         }
81     }
82
83     public int getNumberOfThreads() {
84         return threadCount;
85     }
86
87     public void waitForSlot() {
88         if (tasks.size() > maxQueueSize) {
89             synchronized (waitForSlotSync) {
90                 try {
91                     waitForSlotSync.wait();
92                 } catch (Exception err) {
93                     err.printStackTrace();
94                 }
95             }
96         }
97     }
98
99     public boolean isEmpty() {
100         if (this.threadCount == 0) {
101             return true;
102         }
103         return false;
104     }
105
106     public void setMaxQueueSize(int size) {
107         this.maxQueueSize = size;
108     }
109
110 }