延迟指定时间执行任务的记录

/**
 * for     测试scheduled方法  延迟执行任务的方法
 * get    先执行任务之后的代码,指定时间后执行任务代码(遇见新的线程,只要时间到就执行任务代码)
 * 
 * @author fangguanqgiang
 *
 * 2018年6月7日
 */
public class TestScheduledThreadPool {
public static void main(String[] args) throws ExecutionException, InterruptedException {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
        testTask(pool);
    }


@SuppressWarnings("static-access")
private static void testTask(ScheduledExecutorService pool)
throws InterruptedException {
System.out.println("程序执行的时间:"+new Timestamp(System.currentTimeMillis()));
        pool.schedule(new Runnable() {

@Override
public void run() {
  System.out.println("执行任务的时间:"+new Timestamp(System.currentTimeMillis()));
}
}, 5, TimeUnit.SECONDS);//延迟5秒执行任务
        
        Thread thread = new Thread();
        thread.sleep(6*1000);
        System.err.println("最后一行代码执行的时间:"+new Timestamp(System.currentTimeMillis()));
}
}

更多推荐

ScheduledThreadPool中的schedule方法