安排多次在Java中启动方法

时间:2014-08-27 09:31:13

标签: java scheduled-tasks scheduling reminders

我正在制作一个程序,在管理员指定的特定时间将文件上传到服务器,管理员输入多个值(小时,分钟)。

示例:

[Hours,Minutes]=  [2,12] [ 2,15],[ 5,20 ] 

我将这些值保存在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 Auto-generated catch block
        e.printStackTrace();}

        //Get the Date corresponding to 11:01:00 pm today.
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH");
         Format formatter = new SimpleDateFormat("m");
         Format sec=new SimpleDateFormat("s");

        /*heur=getList().get(i).substring(0, getList().get(i).indexOf(substr));
        minute=getList().get(i).substring(getList().get(i).indexOf(substr) + substr.length());
        System.out.println("Time selected is: "+heur+","+minute);*/
        while (i<lines.size()) {
            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(sdf.format(calendar.getTime()));
            System.out.println(Integer.parseInt(formatter.format(new Date())));
            if(Integer.parseInt(sdf.format(calendar.getTime()))==Integer.parseInt(heur)&&(Integer.parseInt(formatter.format(new Date()))==Integer.parseInt(minute))){


        System.out.println(Integer.parseInt(heur)+"H"+ Integer.parseInt(minute));

        calendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(heur) );
        calendar.set(Calendar.MINUTE, Integer.parseInt(minute));
        calendar.set(Calendar.SECOND, 0);
        Date time = calendar.getTime();

        timer = new Timer();
        timer.schedule(new RemindTask(), time);
        i++;
            }}
        i=1;

        start();


        /*  timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);*/
    }

    class RemindTask extends TimerTask  {

      public void run() {


          up.Uplaod();

        long start = new Date().getTime();
            long end=0; 
            int numIndexed=0;
            boolean cond=true;
            end = new Date().getTime();
            cond=false;

           // System.out.println("Indexing " + numIndexed + " files took "
             // + (end - start) + " milliseconds");

            timer.cancel(); 
            //Terminate the timer thread    

      }

我运行此方法来安排执行上传。它工作两次,之后我收到一个错误:

  

线程中的异常&#34; AWT-EventQueue-0&#34; java.lang.NumberFormatException:对于输入字符串:&#34; Hours&#34;
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    在java.lang.Integer.parseInt(未知来源)
    在java.lang.Integer.parseInt(未知来源)
    在Reminder.start(Reminder.java:64)
    在Reminder.start(Reminder.java:80)
    at csvFileUploadMulti $ 4.actionPerformed(csvFileUploadMulti.java:269)
    在javax.swing.AbstractButton.fireActionPerformed(未知来源)
    at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)
    在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)
    在javax.swing.DefaultButtonModel.setPressed(未知来源)
    在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)
    在java.awt.Component.processMouseEvent(未知来源)
    在javax.swing.JComponent.processMouseEvent(未知来源)
    在java.awt.Component.processEvent(未知来源)
    在java.awt.Container.processEvent(未知来源)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    在java.awt.EventQueue.access $ 400(未知来源)
    在java.awt.EventQueue $ 3.run(未知来源)
    在java.awt.EventQueue $ 3.run(未知来源)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)
    在java.awt.EventQueue $ 4.run(未知来源)
    在java.awt.EventQueue $ 4.run(未知来源)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    在java.awt.EventDispatchThread.run(未知来源)

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

不要重新发明轮子。试试cron