ab059d9e6480d393f98f8ba3e279dd2fe5c76ec9
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLThreadPool.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.md.sal.dom.xsql;
10
11 import java.util.LinkedList;
12
13 public class XSQLThreadPool {
14     private LinkedList<Runnable> tasks = new LinkedList<>();
15     private int threadCount = 0;
16     private int maxThreadCount = 10;
17     private String threadPoolName = "Simple Thread Pool";
18     private int waitTimeForIdle = 10000;
19     private int maxQueueSize = -1;
20     public Object waitForSlotSync = new Object();
21
22     public XSQLThreadPool(int _maxThreadCount, String name,
23         int _waitTimeForIdle) {
24         this.maxThreadCount = _maxThreadCount;
25         this.threadPoolName = name;
26         this.waitTimeForIdle = _waitTimeForIdle;
27     }
28
29     public void addTask(Runnable r) {
30         synchronized (tasks) {
31             tasks.add(r);
32             tasks.notifyAll();
33             if (threadCount < maxThreadCount) {
34                 threadCount++;
35                 new WorkerThread(threadCount).start();
36             }
37         }
38     }
39
40     private class WorkerThread extends Thread {
41
42         private long lastTimeExecuted = System.currentTimeMillis();
43
44         public WorkerThread(int threadNumber) {
45             super(
46                 "Thread #" + threadNumber + " Of Threadpool " + threadPoolName);
47         }
48
49         public void run() {
50             Runnable runthis = null;
51             while (true) {
52                 runthis = null;
53                 if (maxQueueSize != -1) {
54                     synchronized (waitForSlotSync) {
55                         if (tasks.size() < maxQueueSize) {
56                             waitForSlotSync.notifyAll();
57                         }
58                     }
59                 }
60                 synchronized (tasks) {
61                     if (tasks.isEmpty()) {
62                         try {
63                             tasks.wait(2000);
64                         } catch (Exception err) {
65                         }
66                     }
67
68                     if (!tasks.isEmpty()) {
69                         runthis = tasks.removeFirst();
70                     }
71                 }
72                 if (runthis != null) {
73                     try {
74                         runthis.run();
75                     } catch (Exception err) {
76                         err.printStackTrace();
77                     }
78                     lastTimeExecuted = System.currentTimeMillis();
79                 }
80                 if (System.currentTimeMillis() - lastTimeExecuted
81                     > waitTimeForIdle) {
82                     break;
83                 }
84             }
85             synchronized (tasks) {
86                 threadCount--;
87             }
88         }
89     }
90
91     public int getNumberOfThreads() {
92         return threadCount;
93     }
94
95     public void waitForSlot() {
96         if (tasks.size() > maxQueueSize) {
97             synchronized (waitForSlotSync) {
98                 try {
99                     waitForSlotSync.wait();
100                 } catch (Exception err) {
101                     err.printStackTrace();
102                 }
103             }
104         }
105     }
106
107     public boolean isEmpty() {
108         if (this.threadCount == 0) {
109             return true;
110         }
111         return false;
112     }
113
114     public void setMaxQueueSize(int size) {
115         this.maxQueueSize = size;
116     }
117
118 }