Bug 6795 - Deprecated sal-dom-xsql
[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 /**
14  * To be removed in Nitrogen
15  */
16 @Deprecated
17 public class XSQLThreadPool {
18     private LinkedList<Runnable> tasks = new LinkedList<>();
19     private int threadCount = 0;
20     private int maxThreadCount = 10;
21     private String threadPoolName = "Simple Thread Pool";
22     private int waitTimeForIdle = 10000;
23     private int maxQueueSize = -1;
24     public Object waitForSlotSync = new Object();
25
26     public XSQLThreadPool(int _maxThreadCount, String name,
27         int _waitTimeForIdle) {
28         this.maxThreadCount = _maxThreadCount;
29         this.threadPoolName = name;
30         this.waitTimeForIdle = _waitTimeForIdle;
31     }
32
33     public void addTask(Runnable r) {
34         synchronized (tasks) {
35             tasks.add(r);
36             tasks.notifyAll();
37             if (threadCount < maxThreadCount) {
38                 threadCount++;
39                 new WorkerThread(threadCount).start();
40             }
41         }
42     }
43
44     private class WorkerThread extends Thread {
45
46         private long lastTimeExecuted = System.currentTimeMillis();
47
48         public WorkerThread(int threadNumber) {
49             super(
50                 "Thread #" + threadNumber + " Of Threadpool " + threadPoolName);
51         }
52
53         public void run() {
54             Runnable runthis = null;
55             while (true) {
56                 runthis = null;
57                 if (maxQueueSize != -1) {
58                     synchronized (waitForSlotSync) {
59                         if (tasks.size() < maxQueueSize) {
60                             waitForSlotSync.notifyAll();
61                         }
62                     }
63                 }
64                 synchronized (tasks) {
65                     if (tasks.isEmpty()) {
66                         try {
67                             tasks.wait(2000);
68                         } catch (Exception err) {
69                         }
70                     }
71
72                     if (!tasks.isEmpty()) {
73                         runthis = tasks.removeFirst();
74                     }
75                 }
76                 if (runthis != null) {
77                     try {
78                         runthis.run();
79                     } catch (Exception err) {
80                         err.printStackTrace();
81                     }
82                     lastTimeExecuted = System.currentTimeMillis();
83                 }
84                 if (System.currentTimeMillis() - lastTimeExecuted
85                     > waitTimeForIdle) {
86                     break;
87                 }
88             }
89             synchronized (tasks) {
90                 threadCount--;
91             }
92         }
93     }
94
95     public int getNumberOfThreads() {
96         return threadCount;
97     }
98
99     public void waitForSlot() {
100         if (tasks.size() > maxQueueSize) {
101             synchronized (waitForSlotSync) {
102                 try {
103                     waitForSlotSync.wait();
104                 } catch (Exception err) {
105                     err.printStackTrace();
106                 }
107             }
108         }
109     }
110
111     public boolean isEmpty() {
112         if (this.threadCount == 0) {
113             return true;
114         }
115         return false;
116     }
117
118     public void setMaxQueueSize(int size) {
119         this.maxQueueSize = size;
120     }
121
122 }