Eugene
Eugene

Reputation: 263

Timer function not running. Java

I have a question relating to the timer function. I have managed to find the cause of my problem, but I'm not sure on how to address it. I will give you an overview of my function. It will first execute the cost() function, with a background thread working. However, what I realize was that my cost() function failed to load right at the beginning. Secondly, it's program to run every 60 secs which it failed as well. I check my code for my cost() function and it works fine if I call it down without the timer function. Could it be my Opencsv() function? The question is it due to constraint of the timer function or is there ways to address this issue?

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 class file:

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

  }
}

Cost Class:

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 {

  }
  }

Upvotes: 0

Views: 1883

Answers (3)

Poindexter
Poindexter

Reputation: 2425

I think your bug is that you never call Cost's run() method, you are not just overriding it, you are hiding it. Try something like this:

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

Although, as others point out, you should look into the Executor Service.

Upvotes: -1

Eugene
Eugene

Reputation: 120848

I really think that your "bug" is not here, but somewhere else. Also you should be really looking at

ScheduledThreadPoolExecutor

instead of the Timer, it would be something like this :

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

    @Override
    public void run() {
    //Do your stuff in here
    }
}), 60, TimeUnit.SECONDS );

Also may I recommend not to swallow the InterruptedExceptions - there are a lot of posts here on SO on this subject.

Cheers, Eugene.

Upvotes: 1

AlexR
AlexR

Reputation: 115328

It seems that your bug is in class Cost that you have not posted here.

But anyway you have yet another problem here. Why do you create yet another thread inside run() of timer task. It may have sense only if your business logic takes significant time. In your case if your csv file is very large.

Start from simple implementation. Create task that synchronously pareses CSV. Schedule it and see how is it working. If and only if you see that task takes a lot of time thing about using yet another thread. In this case take a look on Executors.

Upvotes: 0

Related Questions