2014-08-28 54 views
0

我使用Cron安排在管理员给定的特定时间内将文件上载到服务器。我创建关于java,其中用户可以选择上载程序的执行时间,并提交所选的值的接口,一旦提交执行以下的方法:cron计划冻结

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(); 



     } 
    } 

}

调度作品它运行,但每当我创建的用户界面冻结,我没有得到任何错误,程序继续运行,但我不能再使用该界面。任何人都可以帮助我。

+0

在EDT _Don't_睡眠; _do_参见[* Swing中的并发*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)和[*如何使用Swing定时器*](http://docs.oracle.com/ JavaSE的/教程/ uiswing /杂项/ timer.html)。 – trashgod 2014-08-28 10:48:51

回答

0
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 

另外,每次格式化您的代码:

  • EclipseCtrl键 + + ˚F

  • 在IntelliJ IDEA的:Ctrl键 + Alt键 + 大号