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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.threading;
  2. import net.javacoding.jspider.core.logging.LogFactory;
  3. import net.javacoding.jspider.core.logging.Log;
  4. import net.javacoding.jspider.core.task.WorkerTask;
  5. /**
  6.  * Implementation of  a Worker Thread.
  7.  * This thread will accept WorkerTasks and execute them.
  8.  *
  9.  * $Id: WorkerThread.java,v 1.11 2003/04/02 20:55:26 vanrogu Exp $
  10.  *
  11.  * @author G黱ther Van Roey
  12.  */
  13. class WorkerThread extends Thread {
  14.     public static final int WORKERTHREAD_IDLE = 0;
  15.     public static final int WORKERTHREAD_BLOCKED = 1;
  16.     public static final int WORKERTHREAD_BUSY = 2;
  17.     /** the current state of this thread - idle, blocked, or busy. */
  18.     protected int state;
  19.     /** Whether this instance is assigned a task. */
  20.     protected boolean assigned;
  21.     /** Whether we should keep alive this thread. */
  22.     protected boolean running;
  23.     /** Threadpool this worker is part of. */
  24.     protected WorkerThreadPool stp;
  25.     /** Task this worker is assigned to. */
  26.     protected WorkerTask task;
  27.     /**
  28.      * Public constructor.
  29.      * @param stp thread pool this worker is part of
  30.      * @param name name of the thread
  31.      * @param i index in the pool
  32.      */
  33.     public WorkerThread(WorkerThreadPool stp, String name, int i) {
  34.         super(stp, name + " " + i);
  35.         this.stp = stp;
  36.         running = false;
  37.         assigned = false;
  38.         state = WORKERTHREAD_IDLE;
  39.     }
  40.     /**
  41.      * Tests whether this worker thread instance can be assigned a task.
  42.      * @return whether we're capable of handling a task.
  43.      */
  44.     public boolean isAvailable() {
  45.         return (!assigned) && running;
  46.     }
  47.     /**
  48.      * Determines whether we're occupied.
  49.      * @return boolean value representing our occupation
  50.      */
  51.     public boolean isOccupied() {
  52.         return assigned;
  53.     }
  54.     /**
  55.      * Method that allows the threadPool to assign the worker a Task.
  56.      * @param task WorkerTask to be executed.
  57.      */
  58.     public synchronized void assign(WorkerTask task) {
  59.         if ( !running ) {
  60.             //SHOULDN'T HAPPEN WITHOUT BUGS
  61.             throw new RuntimeException("THREAD NOT RUNNING, CANNOT ASSIGN TASK !!!");
  62.         }
  63.         if (assigned) {
  64.             //SHOULDN'T HAPPEN WITHOUT BUGS
  65.             throw new RuntimeException("THREAD ALREADY ASSIGNED !!!");
  66.         }
  67.         this.task = task;
  68.         assigned = true;
  69.         notify();
  70.     }
  71.     /**
  72.      * Tells this thread not to accept any new tasks.
  73.      */
  74.     public synchronized void stopRunning() {
  75.         if ( ! running ) {
  76.             throw new RuntimeException ("THREAD NOT RUNNING - CANNOT STOP !");
  77.         }
  78.         if ( assigned ) {
  79.             try {
  80.                 this.wait();
  81.             } catch (InterruptedException e) {
  82.                 Thread.currentThread().interrupt();
  83.             }
  84.         }
  85.         running = false;
  86.         notify();
  87.     }
  88.     /**
  89.      * Returns the state of this worker thread (idle, blocked or busy).
  90.      * @return
  91.      */
  92.     public int getState ( ) {
  93.         return state;
  94.     }
  95.     /**
  96.      * Thread's overridden run method.
  97.      */
  98.     public synchronized void run() {
  99.         running = true;
  100.         Log log = LogFactory.getLog(WorkerThread.class);
  101.         log.debug("Worker thread (" + this.getName() + ") born");
  102.         synchronized (stp) {
  103.             stp.notify();
  104.         }
  105.         while (running) {
  106.             if (assigned) {
  107.                 state = WORKERTHREAD_BLOCKED;
  108.                 task.prepare();
  109.                 state = WORKERTHREAD_BUSY;
  110.                 try {
  111.                     task.execute();
  112.                     task.tearDown();
  113.                 } catch (Exception e) {
  114.                     log.fatal("PANIC! Task " + task + " threw an excpetion!", e);
  115.                     System.exit(1);
  116.                 }
  117.                 synchronized (stp) {
  118.                     assigned = false;
  119.                     task = null;
  120.                     state = WORKERTHREAD_IDLE;
  121.                     stp.notify();
  122.                     this.notify(); // if some thread is blocked in stopRunning();
  123.                 }
  124.             }
  125.             try {
  126.                 wait();
  127.             } catch (InterruptedException e) {
  128.                 Thread.currentThread().interrupt();
  129.             }
  130.         }
  131.         /* notify the thread pool that we died. */
  132.         log.debug("Worker thread (" + this.getName() + ") dying");
  133.     }
  134. }