【问题标题】:Schedule launching a method in Java at multiple times安排在 Java 中多次启动一个方法
【发布时间】:2014-08-27 09:31:13
【问题描述】:

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

例子:

[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    

      }

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

线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:对于输入字符串:“Hours”
在 java.lang.NumberFormatException.forInputString(未知来源)
在 java.lang.Integer.parseInt(未知来源)
在 java.lang.Integer.parseInt(未知来源)
在 Reminder.start(Reminder.java:64)
在 Reminder.start(Reminder.java:80)
在 csvFileUploadMulti$4.actionPerformed(csvFileUploadMulti.java:269)
在 javax.swing.AbstractButton.fireActionPerformed(未知来源)
在 javax.swing.AbstractButton$Handler.actionPerformed(未知来源)
在 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(未知来源)
在 java.awt.Component.dispatchEventImpl(未知来源)
在 java.awt.Container.dispatchEventImpl(未知来源)
在 java.awt.Component.dispatchEvent(未知来源)
在 java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)
在 java.awt.LightweightDispatcher.processMouseEvent(未知来源)
在 java.awt.LightweightDispatcher.dispatchEvent(未知来源)
在 java.awt.Container.dispatchEventImpl(未知来源)
在 java.awt.Window.dispatchEventImpl(未知来源)
在 java.awt.Component.dispatchEvent(未知来源)
在 java.awt.EventQueue.dispatchEventImpl(未知来源)
在 java.awt.EventQueue.access$400(未知来源)
在 java.awt.EventQueue$3.run(未知来源)
在 java.awt.EventQueue$3.run(未知来源)
在 java.security.AccessController.doPrivileged(Native Method)
在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源)
在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源)
在 java.awt.EventQueue$4.run(未知来源)
在 java.awt.EventQueue$4.run(未知来源)
在 java.security.AccessController.doPrivileged(Native Method)
在 java.security.ProtectionDomain$1.doIntersectionPrivilege(未知来源)
在 java.awt.EventQueue.dispatchEvent(未知来源)
在 java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)
在 java.awt.EventDispatchThread.pumpEventsForFilter(未知来源)
在 java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源)
在 java.awt.EventDispatchThread.pumpEvents(未知来源)
在 java.awt.EventDispatchThread.pumpEvents(未知来源)
在 java.awt.EventDispatchThread.run(未知来源)

有人可以帮帮我吗?

【问题讨论】:

  • NumberFormatException:对于输入字符串:“小时”
  • 字符串"Hours",不出所料,无法解析为int。更改应该提取实际数字的逻辑。
  • 看起来你的 csv 有带有列名的标题。您可能应该跳过它。
  • 顺便说一句,从 Java 7 开始,有一种单行方法可以读取文件中的所有行:Files.readAllLines。您可以通过 lines.remove(0) 跟进并解决您的问题。
  • 谢谢你我解决了这个问题,谢谢你

标签: java scheduled-tasks scheduling reminders


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-12-20
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-17
相关资源
最近更新 更多