1 package org.opendaylight.controller.md.sal.dom.xsql;
3 import java.util.LinkedList;
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();
14 public XSQLThreadPool(int _maxThreadCount, String name,
15 int _waitTimeForIdle) {
16 this.maxThreadCount = _maxThreadCount;
17 this.threadPoolName = name;
18 this.waitTimeForIdle = _waitTimeForIdle;
21 public void addTask(Runnable r) {
22 synchronized (tasks) {
25 if (threadCount < maxThreadCount) {
27 new WorkerThread(threadCount).start();
32 private class WorkerThread extends Thread {
34 private long lastTimeExecuted = System.currentTimeMillis();
36 public WorkerThread(int threadNumber) {
38 "Thread #" + threadNumber + " Of Threadpool " + threadPoolName);
42 Runnable runthis = null;
45 if (maxQueueSize != -1) {
46 synchronized (waitForSlotSync) {
47 if (tasks.size() < maxQueueSize) {
48 waitForSlotSync.notifyAll();
52 synchronized (tasks) {
53 if (tasks.isEmpty()) {
56 } catch (Exception err) {
60 if (!tasks.isEmpty()) {
61 runthis = tasks.removeFirst();
64 if (runthis != null) {
67 } catch (Exception err) {
68 err.printStackTrace();
70 lastTimeExecuted = System.currentTimeMillis();
72 if (System.currentTimeMillis() - lastTimeExecuted
77 synchronized (tasks) {
83 public int getNumberOfThreads() {
87 public void waitForSlot() {
88 if (tasks.size() > maxQueueSize) {
89 synchronized (waitForSlotSync) {
91 waitForSlotSync.wait();
92 } catch (Exception err) {
93 err.printStackTrace();
99 public boolean isEmpty() {
100 if (this.threadCount == 0) {
106 public void setMaxQueueSize(int size) {
107 this.maxQueueSize = size;