
de.mhus.lib.core.MThreadDaemon Maven / Gradle / Ivy
/*
* ./core/de/mhu/lib/AThread.java
* Copyright (C) 2002-2004 Mike Hummel
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package de.mhus.lib.core;
import java.util.Vector;
/**
* MThreadDaemon class.
*
* @author hummel
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
* @version $Id: $Id
*/
public class MThreadDaemon extends MThread implements Runnable {
/**
* Constructor for MThreadDaemon.
*/
public MThreadDaemon() {
super();
// TODO Auto-generated constructor stub
}
/**
* Constructor for MThreadDaemon.
*
* @param _task a {@link java.lang.Runnable} object.
* @param _name a {@link java.lang.String} object.
*/
public MThreadDaemon(Runnable _task, String _name) {
super(_task, _name);
// TODO Auto-generated constructor stub
}
/**
* Constructor for MThreadDaemon.
*
* @param _task a {@link java.lang.Runnable} object.
*/
public MThreadDaemon(Runnable _task) {
super(_task);
// TODO Auto-generated constructor stub
}
/**
* Constructor for MThreadDaemon.
*
* @param _name a {@link java.lang.String} object.
*/
public MThreadDaemon(String _name) {
super(_name);
// TODO Auto-generated constructor stub
}
private static Vector pool = new Vector();
private static ThreadGroup group = new ThreadGroup("AThreadDeamon");
/** {@inheritDoc} */
@Override
public MThreadDaemon start() {
tc = start(this, name);
return this;
}
private static ThreadContainer start(MThreadDaemon _task, String _name) {
// search free thread
ThreadContainer tc = null;
synchronized (pool) {
for (int i = 0; i < pool.size(); i++)
if (!pool.elementAt(i).isWorking()) {
tc = pool.elementAt(i);
break;
}
if (tc == null) {
tc = new ThreadContainer(group, "AT_" + pool.size());
tc.setDaemon(true);
tc.start();
pool.addElement(tc);
}
log.t("###: NEW THREAD",tc.getId());
tc.setName(_name);
tc.newWork(_task);
}
return tc;
}
/**
* poolClean.
*
* @param pendingTime a long.
*/
public static void poolClean(long pendingTime) {
synchronized (pool) {
ThreadContainer[] list = pool
.toArray(new ThreadContainer[pool.size()]);
for (int i = 0; i < list.length; i++) {
long sleep = list[i].getSleepTime();
if (sleep != 0 && sleep <= pendingTime) {
pool.remove(list[i]);
list[i].stopRunning();
}
}
}
}
/**
* poolClean.
*/
public static void poolClean() {
synchronized (pool) {
ThreadContainer[] list = pool
.toArray(new ThreadContainer[pool.size()]);
for (int i = 0; i < list.length; i++) {
if (!list[i].isWorking()) {
pool.remove(list[i]);
list[i].stopRunning();
}
}
}
}
/**
* poolSize.
*
* @return a int.
*/
public static int poolSize() {
synchronized (pool) {
return pool.size();
}
}
/**
* poolWorkingSize.
*
* @return a int.
*/
public static int poolWorkingSize() {
int size = 0;
synchronized (pool) {
ThreadContainer[] list = pool
.toArray(new ThreadContainer[pool.size()]);
for (int i = 0; i < list.length; i++) {
if (list[i].isWorking())
size++;
}
}
return size;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy