All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snmp4j.util.TaskScheduler Maven / Gradle / Ivy

There is a newer version: 3.8.2
Show newest version
/*_############################################################################
  _## 
  _##  SNMP4J 2 - TaskScheduler.java  
  _## 
  _##  Copyright (C) 2003-2016  Frank Fock and Jochen Katz (SNMP4J.org)
  _##  
  _##  Licensed under the Apache License, Version 2.0 (the "License");
  _##  you may not use this file except in compliance with the License.
  _##  You may obtain a copy of the License at
  _##  
  _##      http://www.apache.org/licenses/LICENSE-2.0
  _##  
  _##  Unless required by applicable law or agreed to in writing, software
  _##  distributed under the License is distributed on an "AS IS" BASIS,
  _##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  _##  See the License for the specific language governing permissions and
  _##  limitations under the License.
  _##  
  _##########################################################################*/


package org.snmp4j.util;

import java.util.LinkedList;
import org.snmp4j.log.LogFactory;
import org.snmp4j.log.LogAdapter;

/**
 * The TaskScheduler uses a ThreadPool to recurrent
 * execute SchedulerTasks.
 *
 * @author Frank Fock
 * @version 1.6
 * @since 1.6
 */
public class TaskScheduler implements Runnable {

  private LogAdapter logger = LogFactory.getLogger(TaskScheduler.class);

  private static final long DEFAULT_SCHEDULER_TIMEOUT = 5;

  private LinkedList tasks = new LinkedList();
  private ThreadPool threadPool;
  private boolean stop;
  protected long schedulerTimeout = DEFAULT_SCHEDULER_TIMEOUT;

  /**
   * Creates a TaskScheduler that uses the supplied
   * ThreadPool to execute tasks.
   *
   * @param threadPool
   *    a ThreadPool.
   */
  public TaskScheduler(ThreadPool threadPool) {
    this.threadPool = threadPool;
  }

  /**
   * Adds a task to the scheduler.
   * @param task
   *    a SchedulerTask.
   */
  public synchronized void addTask(SchedulerTask task) {
    tasks.addLast(task);
    notify();
  }

  /**
   * Removes a task from the scheduler.
   * @param task
   *    the SchedulerTask to be removed from the scheduler
   * @return
   *    true if the task could be removed.
   */
  public synchronized boolean removeTask(SchedulerTask task) {
    return tasks.remove(task);
  }

  /**
   * Removes all tasks.
   */
  public synchronized void clear() {
    tasks.clear();
  }

  /**
   * Runs the scheduler. While in this method tasks are scheduled on the
   * internal thread pool. The scheduler tries to schedule task fairly.
   */
  public void run() {
    while (!stop) {
      boolean readyToRun = false;
      synchronized (this) {
        for (int i=0; itrue to stop the scheduler.
   */
  public void setStop(boolean stop) {
    this.stop = stop;
  }

  /**
   * Checks if the scheduler is (to be) stopped.
   * @return
   *    true if the scheduler has been stopped or is being stopped.
   */
  public boolean isStop() {
    return stop;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy