线程方式
public class Main{
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
while (true) {
System.out.println("执行任务...");
try {
Thread.sleep(1000);//任务间隔 1s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
Timer 和 TimerTask
public class Main{
public static void main(String[] args) {
TimerTask task = new TimerTask()