package com.tom.servlet;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TimerTest {
private static int i = 0;
public static void initTimer(Long seconds) {
TimerTest.schedule(new TimerTask() {
public void run() {
callUsers();
}
}, 0, seconds*1000);
}
public static void callUsers() {
System.out.println("Filter refreshed: " + ++i + " times.");
}
public static void schedule(TimerTask task, long delay, long period) {
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(task, delay, period,
TimeUnit.MILLISECONDS);
}
}
class TimerTest01 extends TimerTask {
private static int i = 0;
@Override
public void run() {
System.out.println("Servlet refreshed: " + ++i + " times.");
}
}
class TimerTest02 extends TimerTask {
private static int i = 0;
@Override
public void run() {
System.out.println("Listener refreshed: " + ++i + " times.");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页