⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queuedthreadpool.java

📁 是离开的肌肤了卡机是离开的就富利卡及是了的开发及拉考试及的福利科技阿斯利康的肌肤莱卡及时的离开福建阿斯顿发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// ========================================================================// Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// ========================================================================package org.mortbay.thread;import java.io.Serializable;import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import org.mortbay.component.AbstractLifeCycle;import org.mortbay.log.Log;/* ------------------------------------------------------------ *//** A pool of threads. * <p> * Avoids the expense of thread creation by pooling threads after * their run methods exit for reuse. * <p> * If an idle thread is available a job is directly dispatched, * otherwise the job is queued.  After queuing a job, if the total * number of threads is less than the maximum pool size, a new thread  * is spawned. * <p> * @author Greg Wilkins <gregw@mortbay.com> */public class QueuedThreadPool extends AbstractLifeCycle implements Serializable, ThreadPool{    private String _name;    private Set _threads;    private List _idle;    private Runnable[] _jobs;    private int _nextJob;    private int _nextJobSlot;    private int _queued;    private int _maxQueued;        private boolean _daemon;    private int _id;    private final Object _lock = new Lock();    private final Object _threadsLock = new Lock();    private final Object _joinLock = new Lock();    private long _lastShrink;    private int _maxIdleTimeMs=60000;    private int _maxThreads=250;    private int _minThreads=2;    private boolean _warned=false;    private int _lowThreads=0;    private int _priority= Thread.NORM_PRIORITY;    private int _spawnOrShrinkAt=0;    private int _maxStopTimeMs;        /* ------------------------------------------------------------------- */    /* Construct     */    public QueuedThreadPool()    {        _name="qtp-"+hashCode();    }        /* ------------------------------------------------------------------- */    /* Construct     */    public QueuedThreadPool(int maxThreads)    {        this();        setMaxThreads(maxThreads);    }    /* ------------------------------------------------------------ */    /** Run job.     * @return true      */    public boolean dispatch(Runnable job)     {          if (!isRunning() || job==null)            return false;        PoolThread thread=null;        boolean spawn=false;                    synchronized(_lock)        {            // Look for an idle thread            int idle=_idle.size();            if (idle>0)                thread=(PoolThread)_idle.remove(idle-1);            else            {                // queue the job                _queued++;                if (_queued>_maxQueued)                    _maxQueued=_queued;                _jobs[_nextJobSlot++]=job;                if (_nextJobSlot==_jobs.length)                    _nextJobSlot=0;                if (_nextJobSlot==_nextJob)                {                    // Grow the job queue                    Runnable[] jobs= new Runnable[_jobs.length+_maxThreads];                    int split=_jobs.length-_nextJob;                    if (split>0)                        System.arraycopy(_jobs,_nextJob,jobs,0,split);                    if (_nextJob!=0)                        System.arraycopy(_jobs,0,jobs,split,_nextJobSlot);                                        _jobs=jobs;                    _nextJob=0;                    _nextJobSlot=_queued;                }                                  spawn=_queued>_spawnOrShrinkAt;            }        }                if (thread!=null)        {            thread.dispatch(job);        }        else if (spawn)        {            newThread();        }        return true;    }    /* ------------------------------------------------------------ */    /** Get the number of idle threads in the pool.     * @see #getThreads     * @return Number of threads     */    public int getIdleThreads()    {        return _idle==null?0:_idle.size();    }        /* ------------------------------------------------------------ */    /**     * @return low resource threads threshhold     */    public int getLowThreads()    {        return _lowThreads;    }        /* ------------------------------------------------------------ */    /**     * @return maximum queue size     */    public int getMaxQueued()    {        return _maxQueued;    }        /* ------------------------------------------------------------ */    /** Get the maximum thread idle time.     * Delegated to the named or anonymous Pool.     * @see #setMaxIdleTimeMs     * @return Max idle time in ms.     */    public int getMaxIdleTimeMs()    {        return _maxIdleTimeMs;    }        /* ------------------------------------------------------------ */    /** Set the maximum number of threads.     * Delegated to the named or anonymous Pool.     * @see #setMaxThreads     * @return maximum number of threads.     */    public int getMaxThreads()    {        return _maxThreads;    }    /* ------------------------------------------------------------ */    /** Get the minimum number of threads.     * Delegated to the named or anonymous Pool.     * @see #setMinThreads     * @return minimum number of threads.     */    public int getMinThreads()    {        return _minThreads;    }    /* ------------------------------------------------------------ */    /**      * @return The name of the BoundedThreadPool.     */    public String getName()    {        return _name;    }    /* ------------------------------------------------------------ */    /** Get the number of threads in the pool.     * @see #getIdleThreads     * @return Number of threads     */    public int getThreads()    {        return _threads.size();    }    /* ------------------------------------------------------------ */    /** Get the priority of the pool threads.     *  @return the priority of the pool threads.     */    public int getThreadsPriority()    {        return _priority;    }    /* ------------------------------------------------------------ */    public int getQueueSize()    {        return _queued;    }        /* ------------------------------------------------------------ */    /**     * @return the spawnOrShrinkAt  The number of queued jobs (or idle threads) needed      * before the thread pool is grown (or shrunk)     */    public int getSpawnOrShrinkAt()    {        return _spawnOrShrinkAt;    }    /* ------------------------------------------------------------ */    /**     * @param spawnOrShrinkAt The number of queued jobs (or idle threads) needed      * before the thread pool is grown (or shrunk)     */    public void setSpawnOrShrinkAt(int spawnOrShrinkAt)    {        _spawnOrShrinkAt=spawnOrShrinkAt;    }    /* ------------------------------------------------------------ */    /**     * @return maximum total time that stop() will wait for threads to die.     */    public int getMaxStopTimeMs()    {        return _maxStopTimeMs;    }    /* ------------------------------------------------------------ */    /**     * @param stopTimeMs maximum total time that stop() will wait for threads to die.     */    public void setMaxStopTimeMs(int stopTimeMs)    {        _maxStopTimeMs = stopTimeMs;    }    /* ------------------------------------------------------------ */    /**      * Delegated to the named or anonymous Pool.     */    public boolean isDaemon()    {        return _daemon;    }    /* ------------------------------------------------------------ */    public boolean isLowOnThreads()    {        return _queued>_lowThreads;    }    /* ------------------------------------------------------------ */    public void join() throws InterruptedException    {        synchronized (_joinLock)        {            while (isRunning())                _joinLock.wait();        }                // TODO remove this semi busy loop!        while (isStopping())            Thread.sleep(100);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -