java-timer 发表于 2017-08-21 | 分类于 Java 使用Timer类实现12345678910111213141516171819202122232425262728293031323334353637383940public class TimerTest { public static String getStringDate() { Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = formatter.format(currentTime); return dateString; } public static void main(String[] args) throws InterruptedException { final TimerTask timerTask1 = new TimerTask() { @Override public void run() { try { System.out.println("task2 invoked!"+Thread.currentThread().getId()); System.out.println("begin: "+getStringDate()); //Thread.sleep(5000L); } catch (Exception e) { e.printStackTrace(); } } }; final TimerTask timerTask2 = new TimerTask() { @Override public void run() { try { System.out.println("task2 invoked!"+Thread.currentThread().getId()); System.out.println("begin: "+getStringDate()); //Thread.sleep(5000L); } catch (Exception e) { e.printStackTrace(); } } }; Timer timer = new Timer();// 实例化Timer timer.schedule(timerTask1, 8000); timer.schedule(timerTask2, 10000); }} Timer12 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏