Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计时器功能未运行。JAVA_Java_Timer - Fatal编程技术网

计时器功能未运行。JAVA

计时器功能未运行。JAVA,java,timer,Java,Timer,我有一个关于定时器功能的问题。我已经设法找到了问题的原因,但我不确定如何解决它。我将向您概述我的职能。它将首先执行cost()函数,后台线程工作。然而,我意识到我的cost()函数一开始就无法加载。其次,它的程序每60秒运行一次,但也失败了。我检查代码中的cost()函数,如果我在没有计时器函数的情况下调用它,它就可以正常工作。它可能是我的Opencsv()函数吗?问题是由于定时器功能的限制,还是有办法解决这个问题 public static void main(String[] args)

我有一个关于定时器功能的问题。我已经设法找到了问题的原因,但我不确定如何解决它。我将向您概述我的职能。它将首先执行cost()函数,后台线程工作。然而,我意识到我的cost()函数一开始就无法加载。其次,它的程序每60秒运行一次,但也失败了。我检查代码中的cost()函数,如果我在没有计时器函数的情况下调用它,它就可以正常工作。它可能是我的Opencsv()函数吗?问题是由于定时器功能的限制,还是有办法解决这个问题

public static void main(String[] args)  {
  launch(EVschedulerApp.class, args);

  Timer timer = new Timer();
  // timer.scheduleAtFixedRate(new Cost(), 10*1000, 10*1000);

  timer.scheduleAtFixedRate(new Cost() {

      @Override
        public void run() {
        new Thread(new Runnable() {
            public void run() {
              File file = new File("D:/test.csv");
              if(file != null){
                try {
                  Opencsv csv = new Opencsv();

                  csv.Csvreader();
                } catch (IOException ex) {
                  Logger.getLogger(EVschedulerApp.class.getName()).log(Level.SEVERE, null, ex);
                }

              }
              else {

                try {
                  Thread.sleep(1000);
                } catch (InterruptedException e) {}
              }
            }
          }).start();
      }
Opencsv类文件:

public class Opencsv {

  public void Csvreader() throws IOException {
    try {
      // TODO code application logic here

      CSVReader reader = new CSVReader(new FileReader("D:/Test.csv"));

      String [] nextLine;
      while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + " " + nextLine[1]+ " " + nextLine[2]+ " " + nextLine[3]);
      }
    } catch (FileNotFoundException ex) {
      Logger.getLogger(Opencsv.class.getName()).log(Level.SEVERE, null, ex);
    }

  }
}
成本类别:

public class Cost extends TimerTask{

public void run() {
Calendar rightNow = Calendar.getInstance();
Integer hour = rightNow.get(Calendar.HOUR_OF_DAY);
if (hour==23 ) {
try {
  URL tariff = new URL("http://www.******.downloadRealtime=true");
            ReadableByteChannel tar = Channels.newChannel(Test.openStream());
            FileOutputStream fos = new FileOutputStream("Test.csv");
            fos.getChannel().transferFrom(tar, 0, 1<<24);

 } catch (IOException ex) {
            Logger.getLogger(Cost.class.getName()).log(Level.SEVERE, null, ex);
 } 
  }




  else {

  }
  }
公共类成本扩展了TimerTask{
公开募捐{
Calendar rightNow=Calendar.getInstance();
整数hour=rightNow.get(Calendar.hour\u OF_DAY);
如果(小时==23){
试一试{
URL费率=新URL(“http://www.******.downloadRealtime=true”);
ReadableByteChannel tar=Channels.newChannel(Test.openStream());
FileOutputStream fos=新的FileOutputStream(“Test.csv”);

fos.getChannel().transferFrom(tar,0,1您的bug似乎在类
成本中,您尚未在此处发布

但无论如何,这里还有另一个问题。为什么要在计时器任务的
run()
中创建另一个线程。只有当您的业务逻辑占用大量时间时,它才有意义。在您的情况下,如果您的
csv
文件非常大

从简单的实现开始。创建同步排列CSV的任务。安排它并查看它是如何工作的。如果且仅当您看到该任务在使用另一个线程时会花费大量时间。在这种情况下,请查看执行者。我真的认为您的“bug”不是在这里,而是在别的地方。你也应该好好看看

ScheduledThreadPoolExecutor
它不是计时器,而是如下所示:

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(new Runnable() {

    @Override
    public void run() {
    //Do your stuff in here
    }
}), 60, TimeUnit.SECONDS );
另外,我建议不要接受中断例外——这里有很多关于这个主题的帖子

干杯,
Eugene。

我认为您的错误在于您从未调用
Cost
run()
方法,您不仅覆盖了它,还隐藏了它。请尝试以下方法:

timer.scheduleAtFixedRate(new Cost() {
  @Override
    public void run() {

    super.run();  //Added this call to Cost's original method.

    new Thread(new Runnable() {
        public void run() {
          //your code still here
        }
      }).start();
  }

尽管,正如其他人所指出的,您应该研究Executor服务。

我会将我的cost()函数添加到Top。我的csv大约为8kb,没有那么大,但我添加了后台线程,因为没有它,该函数不会解析我的csv文件。我只是添加了我的cost类代码,您能看一下吗?谢谢!我添加了super.run(),但它不起作用,我将尝试执行者服务