Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何每分钟执行一个方法_Java - Fatal编程技术网

Java 如何每分钟执行一个方法

Java 如何每分钟执行一个方法,java,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.

我是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方法来改变太阳的颜色。 如果小时发生变化,我需要每分钟执行一次绘制方法来更改颜色。

您可以使用

Timer timer = new Timer(); 
timer.schedule(new Task(), 60 * 1000);
要在一次之后运行,可以使用

Timer timer = new Timer(); 
timer.schedule(new Task(), 60 * 1000);
要在一次之后运行,可以使用:

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

Runnable drawRunnable = new Runnable() {
    public void run() {
        draw();
    }
};
然后安排执行人:

ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(drawRunnable , 0, 1, TimeUnit.MINUTES);
这样每分钟都会执行一次

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

您可以使用:

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

Runnable drawRunnable = new Runnable() {
    public void run() {
        draw();
    }
};
然后安排执行人:

ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(drawRunnable , 0, 1, TimeUnit.MINUTES);
这样每分钟都会执行一次


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

JavaExecutor API为此作业提供了一个API

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

提供示例代码。

Java执行器API为此作业提供API

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

提供示例代码。

您可以使用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);
}
}
导入静态java.util.concurrent.TimeUnit.*;
类蜂鸣器控制{
专用最终计划执行器服务计划程序=
Executors.newScheduledThreadPool(1);
公共空间{
最终可运行蜂鸣器=新可运行(){
public void run(){System.out.println(“beep”);}
};
最终计划的未来蜂鸣器手柄=
scheduleAtFixedRate(蜂鸣器,10秒,10秒);
scheduler.schedule(新的Runnable(){
public void run(){beeperHandle.cancel(true);}
},60*60,秒);
}
}

您可以使用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);
}
}
导入静态java.util.concurrent.TimeUnit.*;
类蜂鸣器控制{
专用最终计划执行器服务计划程序=
Executors.newScheduledThreadPool(1);
公共空间{
最终可运行蜂鸣器=新可运行(){
public void run(){System.out.println(“beep”);}
};
最终计划的未来蜂鸣器手柄=
scheduleAtFixedRate(蜂鸣器,10秒,10秒);
scheduler.schedule(新的Runnable(){
public void run(){beeperHandle.cancel(true);}
},60*60,秒);
}
}

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

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


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

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


检查此项检查此项检查executor的第二行到底是什么?令牌上的语法错误,请删除这些令牌。对于第2行的第一部分,意思是直到0.1。在这几分钟里,它说插入“Identifier”来完成类体名称,这在EclipseLuna中对我很有用。您导入了所有内容吗?executor的第二行是什么?令牌上的语法错误,请删除这些令牌。对于第2行的第一部分,意思是直到0.1。在这几分钟里,它说插入“Identifier”来完成类体名称,这在EclipseLuna中对我很有用。你导入了所有内容吗?请提供一些关于你在代码中所做操作的信息。是的,你能添加一些注释吗?请提供一些关于你在代码中所做操作的信息。是的,你能添加一些注释吗?