java任务计划
2014-04-27 00:56
405 查看
package com.hongyuan.test; import java.awt.Desktop; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.Charset; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Timer; import java.util.TimerTask; public class TimerTaskTest { public static void main(String[] args) throws ParseException { Timer timer=new Timer(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //延迟指定时间后执行任务(以毫秒为单位) timer.schedule(new TimerTask(){ @Override public void run() { System.out.println("时间已经流逝1秒!!!!"); } }, 1000); //到达指定时间后执行任务 timer.schedule(new TimerTask(){ @Override public void run() { try { //打开浏览器 Desktop.getDesktop().browse(new URI("http://www.baidu.com/")); } catch (IOException | URISyntaxException e) { e.printStackTrace(); } } }, sdf.parse("2014-04-20 10:20:00")); //延迟指定时间后以指定频率开始执行任务 timer.schedule(new TimerTask(){ @Override public void run() { BufferedInputStream in=null; BufferedReader inBr=null; try { //执行系统命令 Process p=Runtime.getRuntime().exec("ping www.baidu.com"); //读取输出 in = new BufferedInputStream(p.getInputStream()); inBr = new BufferedReader(new InputStreamReader(in, Charset.forName("GBK"))); //我的系统字符集为GBK String lineStr=null; while ((lineStr = inBr.readLine()) != null){ //获得命令执行后在控制台的输出信息 System.out.println(lineStr);// 打印输出信息 } //检查命令是否执行失败。 if (p.waitFor() != 0) { if (p.exitValue() == 1)//p.exitValue()==0表示正常结束,1:非正常结束 System.err.println("命令执行失败!"); } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally{ try { inBr.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } }, 10000, 5000); } }
相关文章推荐
- egit(github for eclipse plugin) 使用教程
- Xml配置实现Spring_Hibernate中的声明式事务管理
- java学习路线
- SSH框架_struts2的Action中获取request、response对象的方法
- Java连接SAP ————JCO 3.0技术详解
- java 采集新闻数据
- eclipse maven plugin 插件 安装 和 配置
- MacOS java升级后pycharm 无法使用的解决方案
- java 做采集器 a标签正则表达式
- JDK Proxy实现接口代理
- maven,Failed to create project.java.lang.NullPointerException,at com.genuitec.eclipse.maven.project.
- Java 设计模式之-观察者模式
- springMVC+MyBatis+Spring 整合(2)
- ubuntu 12.04安装jdk
- java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I 异常解决
- 升级版本后报这个异常 : org.springframework.beans.factory.NoUniqueBeanDefinitionException
- Java 自动装箱与拆箱
- javamail 的使用
- Java数组你知多少?
- struts2 跳转类型 result type=chain、dispatcher、redirect(redirect-action)_forever