Java Timer purge() 方法



描述

Java Timer purge() 方法用于从此计时器的任务队列中移除所有已取消的任务。

声明

以下是 java.util.Timer.purge() 方法的声明。

public int purge()

参数

返回值

方法调用返回从队列中移除的任务数量。

异常

以固定速率调度计时器的清除示例

以下示例演示了如何使用 Java Timer purge() 方法清除当前已调度的计时器操作。我们使用 CustomTimerTask 对象创建了一个计时器对象。CustomTimerTask 是扩展 TimerTask 类并实现将在计划时间执行的 run() 方法的自定义类。然后,我们创建了一个计时器对象,并使用 scheduleAtFixedRate() 方法调度了一个任务,该任务从现在开始每 100 毫秒执行一次。然后,使用 cancel() 方法终止计时器操作,并使用 purge() 方法从队列中移除已取消的任务。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer();

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
      System.out.println("purge value :"+timer.purge());
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

输出

让我们编译并运行上述程序,这将产生以下结果。

cancelling timer
purge value :0
working on

以固定速率和日期调度计时器的清除示例

以下示例演示了如何使用 Java Timer purge() 方法清除当前已调度的计时器操作。我们使用 CustomTimerTask 对象创建了一个计时器对象。CustomTimerTask 是扩展 TimerTask 类并实现将在计划时间执行的 run() 方法的自定义类。然后,我们创建了一个作为守护线程的计时器对象,并使用 scheduleAtFixedRate() 方法调度了一个任务,该任务从现在开始每 100 毫秒执行一次。然后,使用 cancel() 方法终止计时器操作,并使用 purge() 方法从队列中移除已取消的任务。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer(true);

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
      System.out.println("purge value :"+timer.purge());
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

输出

让我们编译并运行上述程序,这将产生以下结果。

cancelling timer
purge value :0
working on

以固定日期和初始延迟调度计时器的清除示例

以下示例演示了如何使用 Java Timer purge() 方法清除当前已调度的计时器操作。我们使用 CustomTimerTask 对象创建了一个计时器对象。CustomTimerTask 是扩展 TimerTask 类并实现将在计划时间执行的 run() 方法的自定义类。然后,我们创建了一个具有给定名称的作为守护线程的计时器对象,并使用 scheduleAtFixedRate() 方法调度了一个任务,该任务从现在开始每 100 毫秒执行一次。然后,使用 cancel() 方法终止计时器操作,并使用 purge() 方法从队列中移除已取消的任务。

package com.tutorialspoint;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerDemo {
   public static void main(String[] args) {

      // creating timer task, timer
      TimerTask tasknew = new CustomTimerTask();
      Timer timer = new Timer("test",true);

      // scheduling the task
      timer.scheduleAtFixedRate(tasknew, new Date(), 100);

      // terminating the timer
      System.out.println("cancelling timer");
      timer.cancel();
      System.out.println("purge value :"+timer.purge());
   }
}

class CustomTimerTask extends TimerTask {

   @Override
   public void run() {
      System.out.println("working on");
   }  
}

输出

让我们编译并运行上述程序,这将产生以下结果。

cancelling timer
purge value :0
working on
java_util_timer.htm
广告
© . All rights reserved.