Package org.chaidb.db.helper

Source Code of org.chaidb.db.helper.DaemonThreadManager

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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.
*
*/
package org.chaidb.db.helper;

import org.apache.log4j.Logger;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.exception.ErrorCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author Ramon Liu
* @author Kurt Sung
*         Date: 2005-6-23
*         Time: 11:59:42
*/
public class DaemonThreadManager {
    private static DaemonThreadManager ourInstance;
    private HashMap threads = new HashMap();
    public static final int WAITTIME = 10000;
    private static final Logger logger = Logger.getLogger(DaemonThreadManager.class);

    public synchronized static DaemonThreadManager getInstance() {
        if (ourInstance == null) {
            ourInstance = new DaemonThreadManager();
        }
        return ourInstance;
    }

    private DaemonThreadManager() {
    }

    synchronized void register(AbstractDaemonThread thread) throws ChaiDBException {
        if (threads.containsKey(thread.getName())) {
            logger.warn("There is already a daemon thead named " + thread.getName());
            throw new ChaiDBException(ErrorCode.FAILT_TO_START_DAEMONTHREAD);
        } else {
            threads.put(thread.getName(), thread);
        }
    }

    synchronized void unregister(String name) {
        threads.remove(name);
    }

    public synchronized AbstractDaemonThread getThread(String name) {
        return (AbstractDaemonThread) threads.get(name);
    }

    public void shutdown(String name) {
        AbstractDaemonThread thread;

        synchronized (this) {
            thread = (AbstractDaemonThread) threads.get(name);
        }

        if (thread != null) {
            if (!thread.shutdown(WAITTIME)) {
                logger.warn("Daemon thread " + name + " has not been stopped in " + (WAITTIME / 1000) + "s.");
            }
        }
    }

    /**
     * shutdown all of threads which are registered in DaemonThreadManager
     */
    public void shutdownAll() {
        logger.debug("Close all daemon threads.");
        List threadslist = new ArrayList(threads.size());

        synchronized (this) {
            threadslist.addAll(threads.values());
        }

        for (int i = 0; i < threadslist.size(); i++) {
            AbstractDaemonThread thread = (AbstractDaemonThread) threadslist.get(i);
            thread.shutdown(0);
        }

        for (int i = 0; i < threadslist.size(); i++) {
            AbstractDaemonThread thread = (AbstractDaemonThread) threadslist.get(i);
            try {
                thread.join(WAITTIME);
            } catch (InterruptedException e) {
                ;
            }
            if (thread.isAlive()) {
                logger.warn("Daemon thread " + thread.getName() + " has not been stopped in " + (WAITTIME / 1000) + "s.");
            }
        }

        synchronized (this) {
            threads.clear(); //clear to make sure removing all references to threads
        }
    }

}
TOP

Related Classes of org.chaidb.db.helper.DaemonThreadManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.