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

📄 task.java

📁 经典的货郎担问题解决办法
💻 JAVA
字号:
//-----------------------------------------------------------------------------
//  com.coyotegulch.tools
//
//  A Package of Generic Tools
//  
//  Task.java
//  version 1.1.0
//
//  Copyright 2000-2001 Scott Robert Ladd. All rights reserved.
// 
//  For more information about this program, contact:
//
//      Scott Robert Ladd
//      scott@coyotegulch.com
//      http://www.coyotegulch.com
//
//-----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the 
//
//      Free Software Foundation, Inc. 
//      59 Temple Place - Suite 330 
//      Boston, MA 02111-1307, USA.
//-----------------------------------------------------------------------------

/*
** This code has been reformatted (to keep its longest line from
** wrapping an 80 column text window), but not functionally modified, by
** Kent Paul Dolan.  See accompanying document TravellerDoc.html for the
** status of this code for your use.
*/

package com.coyotegulch.tools;

import java.util.*;

public class Task
{
  private HasTasks m_process;
  private long     m_delay;
  private boolean  m_active;
  private Thread   m_thread;
    
  protected Vector m_listeners = new Vector();
    
  private class TaskThread
    extends Thread
  {
    public void run()
    {
      m_active = true;
        
      while (m_active)
      {
        synchronized (m_process)
        {
          m_process.taskHandler();
        }
                
        if (m_listeners.isEmpty() == false)
        {
          for (int i = 0; i < m_listeners.size(); ++i)
          {
            TaskListener listener = (TaskListener)m_listeners.elementAt(i);
            listener.cycleCompleted();
          }
        }
                
        m_thread.yield();

        try
        {
          m_thread.sleep(m_delay);
        }
        catch (InterruptedException e)
        {
          m_active = false;
        }
      }
    }
  }
    
  public Task(HasTasks taskObject, long delay)
  {
    m_process = taskObject;
    m_delay   = delay;
    m_active  = false;
    m_thread  = null;
  }
    
  public void start()
  {
    if (m_thread == null)
    {
      m_thread = new TaskThread();
      m_thread.start();
    }
  }

  public void stop()
  {
    m_active = false;
    m_thread = null;
  }
    
  public void yield()
  {
    if (m_thread != null)
      m_thread.yield();
  }

  public void addTaskListener(TaskListener al)
  {
    m_listeners.addElement(al);
  }

}

⌨️ 快捷键说明

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