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

📄 dynamicthreadpool.java

📁 network management, thread implement
💻 JAVA
字号:
// Developed by Kinva Network Inc. 2000


// Source File Name:   DynamicThreadPool.java

package com.kinva.util.thread;

import java.io.PrintStream;

// Referenced classes of package com.kinva.util.thread:
//            ThreadPool, PoolThreadSource, ThreadPoolEntry

public class DynamicThreadPool extends ThreadPool
{

    public DynamicThreadPool(PoolThreadSource poolthreadsource)
    {
        this(poolthreadsource, 0, -1, 0);
    }

    public DynamicThreadPool(PoolThreadSource poolthreadsource, int i, int j)
    {
        this(poolthreadsource, i, j, (j - i) / 2);
    }

    public DynamicThreadPool(PoolThreadSource poolthreadsource, int i, int j, int k)
    {
        source = poolthreadsource;
        maxThreads = j;
        minThreads = i;
        hysterisis = k;
        initPool();
    }

    public ThreadPoolEntry getEntry()
    {
        ThreadPoolEntry threadpoolentry = getEntry(true);
        overshoot--;
        return threadpoolentry;
    }

    void initPool()
    {
        while(currentCount < minThreads) 
            acquire();
    }

    protected synchronized void acquire()
    {
        if(currentCount < maxThreads || maxThreads < 0)
        {
            addThread(source.createPoolThread());
            currentCount++;
            overshoot = 0;
            if(peakCount < currentCount)
            {
                peakCount = currentCount;
                return;
            }
        } else
        {
            super.acquire();
        }
    }

    protected boolean reClaim(ThreadPoolEntry threadpoolentry)
    {
        if(currentCount <= minThreads)
            return false;
        if(overshoot >= hysterisis)
        {
            System.out.println("Current count = " + currentCount);
            currentCount--;
            return true;
        } else
        {
            overshoot++;
            return false;
        }
    }

    private PoolThreadSource source;
    private int minThreads;
    private int maxThreads;
    private int hysterisis;
    private int peakCount;
    private int currentCount;
    private int overshoot;
}

⌨️ 快捷键说明

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