package jiqunceshi;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class gfdg {
public static void main(String[] args) {
//创建一个只有一个线程的线程池
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
myTask task = new gfdg().new myTask();
//系统启动后立马执行,且每2秒执行一次,如果说执行task时间超过2秒,则自动往后延
// executor.scheduleAtFixedRate(task, 0, 2, TimeUnit.SECONDS);
Timer t=new Timer();
TimerTask tt=new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("****");
}
};
t.schedule(tt, 3000);
}
class myTask implements Runnable{
@Override
public void run() {
System.out.println("线程:"+Thread.currentThread().getName()+" 在"+ new Date(System.currentTimeMillis()) +"执行我的任务....");
}
}
}
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class gfdg {
public static void main(String[] args) {
//创建一个只有一个线程的线程池
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
myTask task = new gfdg().new myTask();
//系统启动后立马执行,且每2秒执行一次,如果说执行task时间超过2秒,则自动往后延
// executor.scheduleAtFixedRate(task, 0, 2, TimeUnit.SECONDS);
Timer t=new Timer();
TimerTask tt=new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("****");
}
};
t.schedule(tt, 3000);
}
class myTask implements Runnable{
@Override
public void run() {
System.out.println("线程:"+Thread.currentThread().getName()+" 在"+ new Date(System.currentTimeMillis()) +"执行我的任务....");
}
}
}