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

📄 worker.java

📁 swing编写的库存管理程序。毕业设计类
💻 JAVA
字号:
/**
 * ========================================
 * JFreeReport : a free Java report library
 * ========================================
 *
 * Project Info:  http://www.jfree.org/jfreereport/index.html
 * Project Lead:  Thomas Morgner;
 *
 * (C) Copyright 2000-2003, by Simba Management Limited and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * -----------
 * Worker.java
 * -----------
 * (C)opyright 2002, 2003 by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Simba Management Limited);
 *
 * $Id: Worker.java,v 1.11 2003/11/15 20:51:15 taqua Exp $
 *
 *
 * Changes
 * -------
 * 05-Feb-2002 : Initial version
 */

package org.jfree.report.util;



/**
 * A simple worker implementation.
 * The worker executes a assigned workload and then sleeps until
 * another workload is set or the worker is killed.
 *
 * @author Thomas Morgner
 */
public class Worker extends Thread
{
  /** the worker's task. */
  private Runnable workload = null;

  /** a flag whether the worker should exit after the processing. */
  private boolean finish = false;

  /** the time in milliseconds beween 2 checks for exit or work requests. */
  private final int sleeptime;
  /** The worker pool, to which this worker is assigned. May be null. */
  private WorkerPool workerPool;

  /**
   * Creates a new worker.
   *
   * @param sleeptime  the time this worker sleeps until he checks for new work.
   */
  public Worker(final int sleeptime)
  {
    this.sleeptime = sleeptime;
    this.setDaemon(true);
    start();
  }

  /**
   * Creates a new worker with an default idle timeout of 2 minutes.
   */
  public Worker()
  {
    this(120000);
  }

  /**
   * Set the next workload for this worker.
   *
   * @param r  the next workload for the worker.
   *
   * @throws IllegalStateException if the worker is not idle.
   */
  public void setWorkload(final Runnable r)
  {
    if (workload != null)
    {
      throw new IllegalStateException("This worker is not idle.");
    }
    Log.debug("Workload set...");
    synchronized (this)
    {
      workload = r;
      Log.debug("Workload assigned: Notified " + getName());
      this.notifyAll();
    }
  }

  /**
   * Kills the worker after he completed his work. Awakens the worker if he's
   * sleeping, so that the worker dies without delay.
   */
  public void finish()
  {
    finish = true;
    // we are evil ..
    // Log.debug ("Finished Thread: " + getName(), new Exception());
    try
    {
      this.interrupt();
    }
    catch (SecurityException se)
    {
      // ignored 
    }
    if (workerPool != null)
    {
      workerPool.workerFinished(this);
    }
  }

  /**
   * Checks, whether this worker has some work to do.
   *
   * @return true, if this worker has no more work and is currently sleeping.
   */
  public boolean isAvailable()
  {
    return (workload == null);
  }

  /**
   * If a workload is set, process it. After the workload is processed,
   * this worker starts to sleep until a new workload is set for the worker
   * or the worker got the finish() request.
   */
  public synchronized void run()
  {
    while (!finish)
    {
      if (workload != null)
      {
        try
        {
          workload.run();
        }
        catch (Exception e)
        {
          Log.error("Worker caught exception on run: ", e);
        }
        workload = null;
        if (workerPool != null)
        {
          workerPool.workerAvailable(this);
        }
      }
//      else
//      {
//        Log.debug ("Nothing to do ...");
//      }
//
//      Log.debug ("Sleeping ..." + this.getName());
      synchronized (this)
      {
        try
        {
          // remove lock
          this.wait(sleeptime);
        }
        catch (InterruptedException ie)
        {
          // ignored
//          Log.debug ("Interrupted ..." + getName());
        }
      }
//      Log.debug ("Wakeup ..." + getName());
    }
  }


//  private static class HeavyWorkLoad implements Runnable
//  {
//    static int task;
//    char test;
//
//    public HeavyWorkLoad(char t)
//    {
//      test = t;
//    }
//
//    public void run()
//    {
//      synchronized (HeavyWorkLoad.class)
//      {
//        try
//        {
//          System.out.println ("HeavyWorkLoad Start...  " + test);
//          for (int i = 0; i < 100; i++ )
//          {
//            System.out.print (test);
//            System.out.flush();
//            Thread.sleep(100);
//            task = i;
//          }
//          System.out.println ("HeavyWorkLoad Finish...");
//        }
//        catch (Exception e)
//        {
//          System.out.println ("HeavyWorkLoad Failed...");
//        }
//      }
//    }
//  }
//
//  public static void main(String[] args)
//  {
//    HeavyWorkLoad wl1 = new HeavyWorkLoad('.');
//    HeavyWorkLoad wl2 = new HeavyWorkLoad('+');
//    Worker worker = new Worker(10000);
//    worker.setWorkload(wl1);
//    wl2.run();
//    while (worker.isAvailable() == false)
//    {
//      try
//      {
//        System.out.println ("1Waiting ...");
//        synchronized (worker)
//        {
//          worker.wait(1000);
//        }
//      }
//      catch (InterruptedException ie)
//      {
//        System.out.println ("1Int ...");
//      }
//    }
//    System.out.println ("Finishing ...");
//    worker.finish();
//    System.out.flush();
//  }

  /**
   * Checks whether this worker has received the signal to finish and die.
   * 
   * @return true, if the worker should finish the work and end the thread.
   */
  public boolean isFinish()
  {
    return finish;
  }

  /**
   * Returns the worker's assigned pool.
   * 
   * @return the worker pool (or null, if the worker is not assigned to a pool).
   */
  public WorkerPool getWorkerPool()
  {
    return workerPool;
  }

  /**
   * Defines the worker's assigned pool.
   * 
   * @param workerPool the worker pool 
   * (or null, if the worker is not assigned to a pool).
   */
  public void setWorkerPool(final WorkerPool workerPool)
  {
    this.workerPool = workerPool;
  }
}

⌨️ 快捷键说明

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