简体   繁体   中英

How to execute a method every minute

I am new to programming in Java and am making a project where there is a background image and a sun. The sun changes colour based on the time in real life.

My code is as listed below:

package demos;
import processing.core.*;

import java.time.LocalDateTime;
import java.util.TimerTask;


public class Trial extends PApplet{
String URL="http://cseweb.ucsd.edu/~minnes/palmTrees.jpg";
PImage backgroundImg;



public void setup(){
    size(200,200);
    backgroundImg = loadImage(URL,"jpg");
}

public void draw(){
      int hour = LocalDateTime.now().getHour();       
      int minute = LocalDateTime.now().getMinute();   

      switch(hour){
      case 13:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(238,238,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 14:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(205,205,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 15:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(255,215,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 16:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(238,201,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 17:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(255,193,37);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 18:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(205  ,133,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 19:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(255,0,0);
          ellipse(width/4,height/5,width/5,height/5); 
          break;
      }
      case 20:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(211,211,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 21:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(169,169,169);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }

      case 22:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(105,105,105);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 23:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(79,79,79);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 00:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(0,0,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 1:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(79,79,79);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 2:
      {
          backgroundImg.resize(0    ,height);
      image( backgroundImg , 0,0);
      fill(105,105,105);
      ellipse(width/4,height/5,width/5,height/5);
      break;
      }
      case 3:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(169,169,169);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 4:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(211,211,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 5:
      {
          backgroundImg.resize(0    ,height);
      image( backgroundImg , 0,0);
      fill(255,0,0);
      ellipse(width/4,height/5,width/5,height/5);
      break;
      }
      case 6:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(205,133,0);
          ellipse(width/4,height/5,width/5,height/5); 
          break;
      }
      case 7:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(255,193,37);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 8:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(121,29,121);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 9:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(111,209,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;
      }
      case 10:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(230,209,0);
          ellipse(width/4,height/5,width/5,height/5);  
          break;
      }

      case 11:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(255,200,0);
          ellipse(width/4,height/5,width/5,height/5);  
          break;
      }

      case 12:
      {
          backgroundImg.resize(0    ,height);
          image( backgroundImg , 0,0);
          fill(255,209,0);
          ellipse(width/4,height/5,width/5,height/5);  
          break;
      }



      default:
      {  backgroundImg.resize(0 ,height);
          image( backgroundImg , 0,0);
          fill(255,209,0);
          ellipse(width/4,height/5,width/5,height/5);
          break;

      }
}
           }     
}

For now it just executes the draw method once to change the colour of the sun. I need the draw method to execute every minute to change colour if the hour changes.

You can use the ScheduledExecutorService :

First you need to create a runnable and put your method in it:

Runnable drawRunnable = new Runnable() {
    public void run() {
        draw();
    }
};

Then schedule an executor:

ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(drawRunnable , 0, 1, TimeUnit.MINUTES);

That way it will be executed every minute.

Just change the time or the TimeUnit to change the time between the executions.

You can use the ScheduledExecutorService

Here is an example from here for more details please refer same link.

Below code beep every ten seconds for an hour, you can change time as per your requirement

 import static java.util.concurrent.TimeUnit.*;
  class BeeperControl {
private final ScheduledExecutorService scheduler =
   Executors.newScheduledThreadPool(1);

public void beepForAnHour() {
    final Runnable beeper = new Runnable() {
            public void run() { System.out.println("beep"); }
        };
    final ScheduledFuture<?> beeperHandle =
        scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
    scheduler.schedule(new Runnable() {
            public void run() { beeperHandle.cancel(true); }
        }, 60 * 60, SECONDS);
}
}

you can use

Timer timer = new Timer(); 
timer.schedule(new Task(), 60 * 1000);

to run after one time

Java Executor API provides an API for this job.

Executors.newScheduledThreadPool() : Creates a fixed size thread pool 
 that supports delayed and periodic task execution. 

Java doc provides sample code.

If you are using SpringMVC 3, you can do as the following

@Scheduled(fixedDelay=1000)
public void doSomething() {
    // something that should execute periodically
}

For more info

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM