如何每分钟执行一次方法

时间:2015-10-12 05:13:11

标签: java

我是Java新手编程的新手,正在创建一个有背景图像和太阳的项目。太阳会根据现实生活中的时间改变颜色。

我的代码如下:

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;

      }
}
           }     
}

现在它只执行一次draw方法来改变太阳的颜色。 如果小时改变,我需要绘制方法每分钟执行一次以改变颜色。

5 个答案:

答案 0 :(得分:12)

您可以使用ScheduledExecutorService

首先,您需要创建一个runnable并将您的方法放入其中:

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

然后安排遗嘱执行人:

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

这样每分钟都会执行。

只需更改时间或TimeUnit即可更改执行之间的时间。

答案 1 :(得分:2)

您可以使用 ScheduledExecutorService

以下是here的示例,有关详细信息,请参阅相同链接。

下面的代码每隔十秒发出一声哔哔声,您可以根据您的要求更改时间

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

答案 2 :(得分:1)

你可以使用

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

一次运行

答案 3 :(得分:1)

Java Executor API 为此作业提供API。

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

Java doc 提供示例代码。

答案 4 :(得分:0)

如果您使用的是SpringMVC 3,则可以执行以下操作

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

了解更多信息

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