WorkerThreadPool.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:4k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.threading;
  2. import net.javacoding.jspider.core.task.DispatcherTask;
  3. import net.javacoding.jspider.core.task.WorkerTask;
  4. /**
  5.  * Thread Pool implementation that will be used for pooling the spider and
  6.  * parser threads.
  7.  *
  8.  * $Id: WorkerThreadPool.java,v 1.7 2003/02/27 16:47:49 vanrogu Exp $
  9.  *
  10.  * @author G黱ther Van Roey
  11.  */
  12. public class WorkerThreadPool extends ThreadGroup {
  13.     /** Task Dispatcher thread associated with this threadpool. */
  14.     protected DispatcherThread dispatcherThread;
  15.     /** Array of threads in the pool. */
  16.     protected WorkerThread[] pool;
  17.     /** Size of the pool. */
  18.     protected int poolSize;
  19.     /**
  20.      * Public constructor
  21.      * @param poolName name of the threadPool
  22.      * @param threadName name for the worker Threads
  23.      * @param poolSize number of threads in the pool
  24.      */
  25.     public WorkerThreadPool(String poolName, String threadName, int poolSize) {
  26.         super(poolName);
  27.         this.poolSize = poolSize;
  28.         dispatcherThread = new DispatcherThread(this, threadName + " dispatcher", this);
  29.         pool = new WorkerThread[poolSize];
  30.         for (int i = 0; i < poolSize; i++) {
  31.             pool[i] = new WorkerThread(this, threadName, i);
  32.             synchronized (this) {
  33.                 try {
  34.                     pool[i].start();
  35.                     wait();
  36.                 } catch (InterruptedException e) {
  37.                     Thread.currentThread().interrupt();
  38.                 }
  39.             }
  40.         }
  41.     }
  42.     /**
  43.      * Assigns a worker task to the pool.  The threadPool will select a worker
  44.      * thread to execute the task.
  45.      * @param task the WorkerTask to be executed.
  46.      */
  47.     public synchronized void assign(WorkerTask task) {
  48.         while (true) {
  49.             for (int i = 0; i < poolSize; i++) {
  50.                 if (pool[i].isAvailable()) {
  51.                     pool[i].assign(task);
  52.                     return;
  53.                 }
  54.             }
  55.             try {
  56.                 wait();
  57.             } catch (InterruptedException e) {
  58.                 Thread.currentThread().interrupt();
  59.             }
  60.         }
  61.     }
  62.     /**
  63.      * Assigns a DispatcherTask to the threadPool.  The dispatcher thread
  64.      * associated with the threadpool will execute it.
  65.      * @param task DispatcherTask that will keep the workers busy
  66.      */
  67.     public void assignGroupTask(DispatcherTask task) {
  68.         dispatcherThread.assign(task);
  69.     }
  70.     /**
  71.      * Returns the percentage of worker threads that are busy.
  72.      * @return int value representing the percentage of busy workers
  73.      */
  74.     public int getOccupation() {
  75.         int occupied = 0;
  76.         for (int i = 0; i < poolSize; i++) {
  77.             WorkerThread thread = pool[i];
  78.             if (thread.isOccupied()) {
  79.                 occupied++;
  80.             }
  81.         }
  82.         return (occupied * 100) / poolSize;
  83.     }
  84.     public int getBlockedPercentage() {
  85.         int counter = 0;
  86.         for (int i = 0; i < poolSize; i++) {
  87.             WorkerThread thread = pool[i];
  88.             if (thread.getState() == WorkerThread.WORKERTHREAD_BLOCKED ) {
  89.                 counter++;
  90.             }
  91.         }
  92.         return (counter * 100) / poolSize;
  93.     }
  94.     public int getBusyPercentage () {
  95.         int counter = 0;
  96.         for (int i = 0; i < poolSize; i++) {
  97.             WorkerThread thread = pool[i];
  98.             if (thread.getState() == WorkerThread.WORKERTHREAD_BUSY) {
  99.                 counter++;
  100.             }
  101.         }
  102.         return (counter * 100) / poolSize;
  103.     }
  104.     public int getIdlePercentage ( ) {
  105.         int counter = 0;
  106.         for (int i = 0; i < poolSize; i++) {
  107.             WorkerThread thread = pool[i];
  108.             if (thread.getState() == WorkerThread.WORKERTHREAD_IDLE ) {
  109.                 counter++;
  110.             }
  111.         }
  112.         return (counter * 100) / poolSize;
  113.     }
  114.     /**
  115.      * Causes all worker threads to die.
  116.      */
  117.     public void stopAll() {
  118.         for (int i = 0; i < pool.length; i++) {
  119.             WorkerThread thread = pool[i];
  120.             thread.stopRunning();
  121.         }
  122.     }
  123.     /**
  124.      * Returns the number of worker threads that are in the pool.
  125.      * @return the number of worker threads in the pool
  126.      */
  127.     public int getSize ( ) {
  128.         return poolSize;
  129.     }
  130. }