2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.md.sal.dom.xsql;
11 import java.util.LinkedList;
14 * To be removed in Nitrogen
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();
26 public XSQLThreadPool(int _maxThreadCount, String name,
27 int _waitTimeForIdle) {
28 this.maxThreadCount = _maxThreadCount;
29 this.threadPoolName = name;
30 this.waitTimeForIdle = _waitTimeForIdle;
33 public void addTask(Runnable r) {
34 synchronized (tasks) {
37 if (threadCount < maxThreadCount) {
39 new WorkerThread(threadCount).start();
44 private class WorkerThread extends Thread {
46 private long lastTimeExecuted = System.currentTimeMillis();
48 public WorkerThread(int threadNumber) {
50 "Thread #" + threadNumber + " Of Threadpool " + threadPoolName);
54 Runnable runthis = null;
57 if (maxQueueSize != -1) {
58 synchronized (waitForSlotSync) {
59 if (tasks.size() < maxQueueSize) {
60 waitForSlotSync.notifyAll();
64 synchronized (tasks) {
65 if (tasks.isEmpty()) {
68 } catch (Exception err) {
72 if (!tasks.isEmpty()) {
73 runthis = tasks.removeFirst();
76 if (runthis != null) {
79 } catch (Exception err) {
80 err.printStackTrace();
82 lastTimeExecuted = System.currentTimeMillis();
84 if (System.currentTimeMillis() - lastTimeExecuted
89 synchronized (tasks) {
95 public int getNumberOfThreads() {
99 public void waitForSlot() {
100 if (tasks.size() > maxQueueSize) {
101 synchronized (waitForSlotSync) {
103 waitForSlotSync.wait();
104 } catch (Exception err) {
105 err.printStackTrace();
111 public boolean isEmpty() {
112 if (this.threadCount == 0) {
118 public void setMaxQueueSize(int size) {
119 this.maxQueueSize = size;