简体   繁体   English

如何每分钟执行一种方法

[英]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. 我是Java编程新手,正在制作一个有背景图像和阳光的项目。 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. 现在,它只需执行一次draw方法即可更改太阳的颜色。 I need the draw method to execute every minute to change colour if the hour changes. 如果小时更改,我需要draw方法每分钟执行一次以更改颜色。

You can use the ScheduledExecutorService : 您可以使用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. 只需更改时间或TimeUnit即可更改两次执行之间的时间。

You can use the ScheduledExecutorService 您可以使用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. Java Executor API为此作业提供了一个API。

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

Java doc provides sample code. Java文档提供了示例代码。

If you are using SpringMVC 3, you can do as the following 如果您使用的是SpringMVC 3,则可以执行以下操作

@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 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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