簡體   English   中英

如何每分鍾執行一種方法

[英]How to execute a method every minute

我是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方法即可更改太陽的顏色。 如果小時更改,我需要draw方法每分鍾執行一次以更改顏色。

您可以使用ScheduledExecutorService

首先,您需要創建一個可運行對象並將其放入其中:

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

然后安排執行者:

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

這樣,它將每分鍾執行一次。

只需更改時間或TimeUnit即可更改兩次執行之間的時間。

您可以使用ScheduledExecutorService

下面是一個例子這里了解更多詳情請參考相同的鏈接。

下面的代碼每十秒鍾發出一聲嗶聲,持續一個小時,您可以根據需要更改時間

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

您可以使用

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

一次運行

Java Executor API為此作業提供了一個API。

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

Java文檔提供了示例代碼。

如果您使用的是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM