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

📄 threadpool.java

📁 彩信网关程序,SP程序,包含移动彩信代码和电信彩信代码!供学习之用
💻 JAVA
字号:
package com.hxyh.sanny.mms.cmcc.base.thread;

public class ThreadPool
{

    private ObjectFIFO idleWorkers;
    private ThreadPoolWorker workerList[];

    public ThreadPool(int numberOfThreads)
    {
        numberOfThreads = Math.max(1, numberOfThreads);
        idleWorkers = new ObjectFIFO(numberOfThreads);
        workerList = new ThreadPoolWorker[numberOfThreads];
        for(int i = 0; i < workerList.length; i++)
            workerList[i] = new ThreadPoolWorker(idleWorkers);

    }

    public ThreadPool(int numberOfThreads, int priority)
    {
        numberOfThreads = Math.max(1, numberOfThreads);
        idleWorkers = new ObjectFIFO(numberOfThreads);
        workerList = new ThreadPoolWorker[numberOfThreads];
        for(int i = 0; i < workerList.length; i++)
            workerList[i] = new ThreadPoolWorker(idleWorkers, priority);

    }

    public void setTimeout(long l)
    {
      for(int i = 0; i < workerList.length; i++)
           workerList[i].setTimeout(l);
    }

    public void execute(Runnable target)
        throws InterruptedException
    {
      // block (forever) until a worker is available
      ThreadPoolWorker worker = (ThreadPoolWorker)idleWorkers.remove();
      //System.out.println("Free Workers Num:"+idleWorkers.getSize()+"/"+idleWorkers.getCapacity());
       worker.process(target);
    }

    public void stopRequestIdleWorkers()
    {
        try
        {
            Object idle[] = idleWorkers.removeAll();
            for(int i = 0; i < idle.length; i++)
                ((ThreadPoolWorker)idle[i]).stopRequest();

        }
        catch(InterruptedException x)
        {
            Thread.currentThread().interrupt();
        }
    }

    public void stopRequestAllWorkers()
    {
        stopRequestIdleWorkers();
        try
        {
            Thread.sleep(250L);
        }
        catch(InterruptedException interruptedexception) { }
        for(int i = 0; i < workerList.length; i++)
            if(workerList[i].isAlive())
                workerList[i].stopRequest();

    }
}

⌨️ 快捷键说明

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