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

📄 boundedthreadpool.java

📁 是离开的肌肤了卡机是离开的就富利卡及是了的开发及拉考试及的福利科技阿斯利康的肌肤莱卡及时的离开福建阿斯顿发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setMaxThreads(int maxThreads)    {        if (isStarted() && maxThreads<_minThreads)            throw new IllegalArgumentException("!minThreads<maxThreads");        _maxThreads=maxThreads;    }    /* ------------------------------------------------------------ */    /** Set the minimum number of threads.     * Delegated to the named or anonymous Pool.     * @see #getMinThreads     * @param minThreads minimum number of threads     */    public void setMinThreads(int minThreads)    {        if (isStarted() && (minThreads<=0 || minThreads>_maxThreads))            throw new IllegalArgumentException("!0<=minThreads<maxThreads");        _minThreads=minThreads;        synchronized (_lock)        {            while (isStarted() && _threads.size()<_minThreads)            {                newThread(null);               }        }    }    /* ------------------------------------------------------------ */    /**      * @param name Name of the BoundedThreadPool to use when naming Threads.     */    public void setName(String name)    {        _name= name;    }    /* ------------------------------------------------------------ */    /** Set the priority of the pool threads.     *  @param priority the new thread priority.     */    public void setThreadsPriority(int priority)    {        _priority=priority;    }    /* ------------------------------------------------------------ */    /* Start the BoundedThreadPool.     * Construct the minimum number of threads.     */    protected void doStart() throws Exception    {        if (_maxThreads<_minThreads || _minThreads<=0)            throw new IllegalArgumentException("!0<minThreads<maxThreads");                _threads=new HashSet();        _idle=new ArrayList();        _queue=new LinkedList();                for (int i=0;i<_minThreads;i++)        {            newThread(null);        }       }    /* ------------------------------------------------------------ */    /** Stop the BoundedThreadPool.     * New jobs are no longer accepted,idle threads are interrupted     * and stopJob is called on active threads.     * The method then waits      * min(getMaxStopTimeMs(),getMaxIdleTimeMs()), for all jobs to     * stop, at which time killJob is called.     */    protected void doStop() throws Exception    {           super.doStop();                for (int i=0;i<100;i++)        {            synchronized (_lock)            {                Iterator iter = _threads.iterator();                while (iter.hasNext())                    ((Thread)iter.next()).interrupt();            }                        Thread.yield();            if (_threads.size()==0)               break;                        try            {                Thread.sleep(i*100);            }            catch(InterruptedException e){}        }        // TODO perhaps force stops        if (_threads.size()>0)            Log.warn(_threads.size()+" threads could not be stopped");                synchronized (_joinLock)        {            _joinLock.notifyAll();        }    }    /* ------------------------------------------------------------ */    protected PoolThread newThread(Runnable job)    {        synchronized(_lock)        {            PoolThread thread =new PoolThread(job);            _threads.add(thread);            thread.setName(_name+"-"+_id++);            thread.start();              return thread;        }    }    /* ------------------------------------------------------------ */    /** Stop a Job.     * This method is called by the Pool if a job needs to be stopped.     * The default implementation does nothing and should be extended by a     * derived thread pool class if special action is required.     * @param thread The thread allocated to the job, or null if no thread allocated.     * @param job The job object passed to run.     */    protected void stopJob(Thread thread, Object job)    {        thread.interrupt();    }        /* ------------------------------------------------------------ */    /** Pool Thread class.     * The PoolThread allows the threads job to be     * retrieved and active status to be indicated.     */    public class PoolThread extends Thread     {        Runnable _job=null;        /* ------------------------------------------------------------ */        PoolThread()        {            setDaemon(_daemon);            setPriority(_priority);        }        /* ------------------------------------------------------------ */        PoolThread(Runnable job)        {            setDaemon(_daemon);            setPriority(_priority);            _job=job;        }        /* ------------------------------------------------------------ */        /** BoundedThreadPool run.         * Loop getting jobs and handling them until idle or stopped.         */        public void run()        {            try            {                Runnable job=null;                synchronized (this)                {                    job=_job;                    _job=null;                }                                while (isRunning())                {                    if (job!=null)                    {                        Runnable todo=job;                        job=null;                        todo.run();                    }                    else                    {                        // No job                        synchronized (_lock)                        {                            // is there a queued job?                            if (_queue.size()>0)                            {                                job=(Runnable)_queue.remove(0);                                continue;                            }                            else                            {                                _warned=false;                                                                // consider shrinking the thread pool                                if (_threads.size()>_maxThreads ||     // we have too many threads  OR                                    _idle.size()>0 &&                  // are there idle threads?                                    _threads.size()>_minThreads)       // AND are there more than min threads?                                {                                    long now = System.currentTimeMillis();                                    if ((now-_lastShrink)>getMaxIdleTimeMs())                                    {                                        _lastShrink=now;                                        return;                                    }                                }                            }                                                           // we are going idle!                            _idle.add(this);                        }                        try                        {                            synchronized (this)                            {                                if (_job==null)                                    this.wait(getMaxIdleTimeMs());                                job=_job;                                _job=null;                            }                        }                        catch (InterruptedException e)                        {                            Log.ignore(e);                        }                        finally                        {                            synchronized (_lock)                            {                                _idle.remove(this);                            }                        }                    }                }            }            finally            {                synchronized (_lock)                {                    _threads.remove(this);                }                                Runnable job=null;                synchronized (this)                {                    job=_job;                }                if (job!=null && isRunning())                    BoundedThreadPool.this.dispatch(job);            }        }                /* ------------------------------------------------------------ */        void dispatch(Runnable job)        {            synchronized (this)            {                if(_job!=null || job==null)                    throw new IllegalStateException();                _job=job;                this.notify();            }        }    }}

⌨️ 快捷键说明

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