欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java任务计划,

来源: javaer 分享于  点击 48474 次 点评:248

java任务计划,


这是一个演示如何使用java执行定时任务的实例,本实例开始运行后不会自动结束,请在运行本实例后手动结束程序。 标签: <无>

代码片段(2)[全屏查看所有代码]

1. [文件] TimerTaskTest.java ~ 3KB     下载(10)    

01 package com.hongyuan.test;
02  
03 import java.awt.Desktop;
04 import java.io.BufferedInputStream;
05 import java.io.BufferedReader;
06 import java.io.IOException;
07 import java.io.InputStreamReader;
08 import java.net.URI;
09 import java.net.URISyntaxException;
10 import java.nio.charset.Charset;
11 import java.text.ParseException;
12 import java.text.SimpleDateFormat;
13 import java.util.Timer;
14 import java.util.TimerTask;
15  
16 public class TimerTaskTest {
17  
18     public static void main(String[] args) throws ParseException {
19          
20         Timer timer=new Timer();
21         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
22          
23         //延迟指定时间后执行任务(以毫秒为单位)
24         timer.schedule(new TimerTask(){
25  
26             @Override
27             public void run() {
28                 System.out.println("时间已经流逝1秒!!!!");
29             }
30              
31         }, 1000);
32          
33         //到达指定时间后执行任务
34         timer.schedule(new TimerTask(){
35  
36             @Override
37             public void run() {
38                 try {
39                     //打开浏览器
40                     Desktop.getDesktop().browse(new URI("http://www.baidu.com/"));
41                 } catch (IOException | URISyntaxException e) {
42                     e.printStackTrace();
43                 }
44             }
45              
46         }, sdf.parse("2014-04-20 10:20:00"));
47          
48         //延迟指定时间后以指定频率开始执行任务
49         timer.schedule(new TimerTask(){
50  
51             @Override
52             public void run() {
53                 BufferedInputStream in=null;
54                 BufferedReader inBr=null;
55                 try {
56                     //执行系统命令
57                     Process p=Runtime.getRuntime().exec("ping www.baidu.com");
58                      
59                     //读取输出
60                     in = new BufferedInputStream(p.getInputStream());
61                     inBr = new BufferedReader(new InputStreamReader(in,
62                             Charset.forName("GBK"))); //我的系统字符集为GBK 
63                     String lineStr=null
64                     while ((lineStr = inBr.readLine()) != null){ 
65                         //获得命令执行后在控制台的输出信息
66                         System.out.println(lineStr);// 打印输出信息 
67                     }
68                      
69                     //检查命令是否执行失败。 
70                     if (p.waitFor() != 0) { 
71                         if (p.exitValue() == 1)//p.exitValue()==0表示正常结束,1:非正常结束 
72                             System.err.println("命令执行失败!"); 
73                     }
74                      
75                 } catch (IOException e) {
76                     e.printStackTrace();
77                 } catch (InterruptedException e) {
78                     e.printStackTrace();
79                 } finally{
80                     try {
81                         inBr.close(); 
82                         in.close();
83                     } catch (IOException e) {
84                         e.printStackTrace();
85                     }
86                 }
87             }
88              
89         }, 10000, 5000);
90          
91     }
92  
93 }

2. [图片] 20140420102859.png    

相关文章

    暂无相关文章
相关栏目:

用户点评