每小时运行一次java函数
我想每小时运行一次功能,每小时向用户发送一次进度截图。我在一个名为sendScreenshot()的函数中设置了这样做的代码 当程序的其余部分正在运行时,如何在后台运行此计时器以每小时调用函数sendScreenshot() 这是我的密码:每小时运行一次java函数,java,schedule,Java,Schedule,我想每小时运行一次功能,每小时向用户发送一次进度截图。我在一个名为sendScreenshot()的函数中设置了这样做的代码 当程序的其余部分正在运行时,如何在后台运行此计时器以每小时调用函数sendScreenshot() 这是我的密码: public int onLoop() throws Exception{ if(getLocalPlayer().getHealth() == 0){ playerHasDied(); } return Calcul
public int onLoop() throws Exception{
if(getLocalPlayer().getHealth() == 0){
playerHasDied();
}
return Calculations.random(200, 300);
}
public void sendScreenShot() throws Exception{
Robot robot = new Robot();
BufferedImage screenshot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
screenshotNumber = getNewestScreenshot();
fileName = new File("C:/Users/%username%/Dreambot/Screenshots/Screenshot" + screenshotNumber +".");
ImageIO.write(screenshot, "JPEG", fileName);
mail.setSubject("Your hourly progress on account " + accName);
mail.setBody("Here is your hourly progress report on account " + accName +". Progress is attached in this mail.");
mail.addAttachment(fileName.toString());
mail.setTo(reciepents);
mail.send();
}
使用:
宁愿使用ScheduledExecutorService
而不是Timer
:
java的
计时器在这里工作正常
根据,也可以使用@Schedule
注释:
@Schedule(hour = "*")
public void doSomething() {
System.out.println("hello world");
}
例如,秒和分钟的值可以为0-59、小时0-23、月1-12
这里还介绍了其他选项。对于这种类型的周期执行,即每天或每小时,您只需使用如下计时器:
public static void main(String[] args) throws InterruptedException {
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 7);
today.set(Calendar.MINUTE, 45);
today.set(Calendar.SECOND, 0);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("I am the timer");
}
};
// timer.schedule(task, today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)); // period: 1 day
timer.schedule(task, today.getTime(), TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS)); // period: 5 seconds
}
此示例将从当前日期到上午7:45每5秒执行一次timetask。
祝你好运。你可以用定时器。有一些库可以用来做这件事…你最好使用一个调度器:这里已经回答了,但这需要一些上下文来查看注释,例如企业服务器、spring等@user1052080此解决方案不需要任何注释。这是为JEE,他最有可能使用JSE,我想这是最优雅的方式。谢谢。我怎样才能让它像后台服务一样运行,这样即使在计算机启动时应用程序也不会运行,但线程会在其定义的时间自动运行?@a.Aleem11如果您想要免费开放源代码或美国黑曜石调度程序,请使用Quartz调度程序。更短,使用Java 8:Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(()->sendScreenShot(),0,1,TimeUnit.HOURS)
。
@Schedule(hour = "*")
public void doSomething() {
System.out.println("hello world");
}
public static void main(String[] args) throws InterruptedException {
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 7);
today.set(Calendar.MINUTE, 45);
today.set(Calendar.SECOND, 0);
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
System.out.println("I am the timer");
}
};
// timer.schedule(task, today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)); // period: 1 day
timer.schedule(task, today.getTime(), TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS)); // period: 5 seconds
}