admin管理员组文章数量:1794759
ScheduledThreadPool中的schedule方法
延迟指定时间执行任务的记录
/** * 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())); }}
本文标签: 方法ScheduledThreadPoolschedule
版权声明:本文标题:ScheduledThreadPool中的schedule方法 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686477518a71904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论