java Timer 定时任务(一)
来源:互联网 发布:陈一发唱歌软件 编辑:程序博客网 时间:2024/05/17 03:13
Java调用shell 执行定时
try {
proc.directory(new File("D:\\"));
Process start =proc.start();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Task scheduled.");
0
indicates normal termination 0表示正常终止,程序改为如下:try {
proc.directory(new File("D:\\"));
Process start =proc.start();
int waitFor = start.waitFor();
if(waitFor==0){
System.out.println(waitFor);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
此程序还会出现问题:
public abstract int waitFor() throws InterruptedException
- causes the current thread to wait, if necessary, until the process represented by this
Process
object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits. - Returns:
the exit value of the process. By convention,
0
indicates normal termination上面的大致意思就是它会是当前线程处于等待状态,如果子线程还没有终止,那么它将一直处于阻塞状态,查看我们项目中的解决方案代码改为如下:
// new MyTimer(5);
ProcessBuilder proc = new ProcessBuilder("notepad.exe", "test");
try {
proc.directory(new File("D:\\"));
Process start =proc.start();
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(start.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(start.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
int waitFor = start.waitFor();
if(waitFor==0){
System.out.println(waitFor);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}通过网上介绍:
假设该程序不断在向标准输出流和标准错误流写数据,而JVM不读取的话,当缓冲区满之后将无法继续写入数据,最终造成阻塞在waitfor()这里。 知道问题所在,我们解决问题就好办了。查看网上说的方法多数是开两个线程在waitfor()命令之前读出窗口的标准输出缓冲区和标准错误流的内容
- Returns:
- java Timer 定时任务(一)
- java Timer 定时任务(一)
- java Timer 定时任务
- Java定时任务Timer
- JAVA定时任务Timer
- Java 定时任务Timer
- Java定时任务Timer
- java Timer 定时任务
- java 定时任务Timer
- java定时任务 timer
- java Timer 定时任务(二)
- java Timer 定时任务(二)
- Java执行定时任务(Timer、Quartz)
- java Timer实现定时任务
- java定时任务池--timer
- Java基础--定时任务Timer
- java定时任务 Timer等
- Java基础--定时任务Timer
- 各种主流浏览器CSS、BUG兼容-
- 使用CSS+JavaScript生成Tips(Bubble)
- spring对事务的捕获
- Excel冻结窗口及设置下拉菜单
- JavaScript函数,语法,定义方式
- java Timer 定时任务(一)
- Sqlite删除列方法
- Apache安装问题:configure: error: APR not found . Please read the documentation
- 基础:Eclipse开发JAVA中如何Debug
- Spring定时任务的几种实现
- 电子设计自动化实验 实验四 波形发生器
- spark mlib官方文档
- Java程序员应该知道的10个调试技巧
- android学习之java常识