cron计划冻结 [英] cron schedule freeze

查看:209
本文介绍了cron计划冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Cron安排在管理员给定的特定时间内将文件上传到服务器。我在java上创建了一个接口,用户可以选择执行上传程序的时间,并提交选择的值,一旦提交,执行以下方法:

  public class提醒{

String minute;
// static int i = 0;
String heur;
String substr =,;
字符串模式;
List< String> list = null;
List< String> lines = new ArrayList<>();


定时器;
FTPUploadFileDemo up = new FTPUploadFileDemo();

public void start()throws IOException {
/ ************从管理员获取保存在CSV文件中的选择值***** **************************************************** ** /
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(C:/Users/BACKENDPC1/Desktop/timer.csv));


String line = null;
while((line = reader.readLine())!= null){
lines.add(line);


}} catch(FileNotFoundException e){
// TODO自动生成的catch块
e.printStackTrace();}
/ ** ********************创建cron模式************************* ****************** /

patterns =;
for(int i = 0; i< lines.size(); i ++){
heur = lines.get(i).substring(0,lines.get(i).indexOf );
minute = lines.get(i).substring(lines.get(i).indexOf(substr)+ substr.length());
System.out.println(选择的时间是:+ heur +,+分钟);
patterns = patterns + minute ++ heur +* * * |;

}
System.out.println(patterns);


//创建调度程序。
Scheduler scheduler = new Scheduler();
//计划任务,每分钟一次。
scheduler.schedule(patterns,new RemindTask());
scheduler.start();
try {
Thread.sleep(1L * 60L * 1000L);
} catch(InterruptedException e){
System.out.println(e);
}
//停止调度程序。
scheduler.stop();

}

类RemindTask扩展TimerTask {

public void run(){



up.Uplaod();



}
}

}



调度工作,它运行,但每次用户界面我创建的冻结,我没有得到任何错误,程序保持运行,但我可以不再使用界面。

$

  public void start()throws IOException {
..............
try {
Thread.sleep(1L * 60L * 1000L);
} catch(InterruptedException e){
System.out.println(e);
}
...............
}

为什么要暂停主线程60秒?调度程序在单独的线程中运行自己的任务,因此您不应该中断主线程的执行。



另外,尝试放置断点并逐步调试程序问题



并且不要这样写数学操作:

  1L * 60L * 1000L 

就足够写:

  1L * 60 * 1000 

,每次格式化您的代码:




  • Eclipse Ctrl + Shift + F

    在IntelliJ IDEA中: Ctrl + Alt + L

    / li>

I'm using Cron to schedule an the upload of a file to the server in specific time given by the adminstrator. i created an interface on java, where the user can choose the time of execution of the upload program, and submit the chosen values, once submitted the following method is executed:

public class Reminder {

String minute;
//static int i=0;
String heur;
String substr=",";
String patterns;
List<String> list = null;
List<String> lines = new ArrayList<>();


Timer timer;
 FTPUploadFileDemo up=new FTPUploadFileDemo();

  public void start() throws IOException {
      /************ Get the chosen values from the administrator saved in a CSV file *********************************************************/
      BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader("C:/Users/BACKENDPC1/Desktop/timer.csv"));


    String line = null;
    while ((line = reader.readLine()) != null) {
        lines.add(line);


    }} catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();}
/**********************create cron patterns *********************************************/

        patterns="";
        for(int i=0; i<lines.size();i++) {
            heur=lines.get(i).substring(0, lines.get(i).indexOf(substr));
            minute=lines.get(i).substring(lines.get(i).indexOf(substr) + substr.length());
            System.out.println("Time selected is: "+heur+","+minute);
            patterns=patterns+minute+" "+heur+" * * *|";

        }
        System.out.println(patterns);


        // Creates the scheduler.
        Scheduler scheduler = new Scheduler();
    // Schedules the task, once every minute.
        scheduler.schedule(patterns,new RemindTask());
        scheduler.start();
        try {
            Thread.sleep(1L * 60L * 1000L);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        // Stops the scheduler.
        scheduler.stop();

    }

   class RemindTask extends TimerTask  {

      public void run() {



          up.Uplaod();



      }
    }

}

the scheduling works and it runs but, every time the user interface i created freeze, i don't get any error and the program keeps running but the i can't use the interface anymore. can any one help me please.

解决方案

public void start() throws IOException {
..............
    try {
        Thread.sleep(1L * 60L * 1000L);
    } catch (InterruptedException e) {
        System.out.println(e);
    }
...............
}

Why you pause main thread for 60 secounds? Scheduler run his own tasks in separate thread, so you shouldn't interrupt execution of main thread.

ALSO, try to put breakpoints and debug your program step by step and localize problem

And don't write math operations like this:

1L * 60L * 1000L

will be enough to write:

1L * 60 * 1000

In addition, every time format your code:

  • In Eclipse: Ctrl + Shift + F

  • In IntelliJ IDEA: Ctrl + Alt + L

这篇关于cron计划冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆