specific scheduleatfixedrate quartz example ejemplo java scheduling

java - scheduleatfixedrate - Llamando a una función cada 10 minutos



scheduler java ejemplo (4)

No soy un experto, solo un principiante. Así que le pido amablemente que escriba un código para mí.

Si tengo dos clases, CLASS A y CLASS B , y dentro de CLASS B hay una función llamada funb() . Quiero llamar a esta función de CLASS A cada diez minutos.

Ya me has dado algunas ideas, sin embargo no lo entendí del todo.

¿Puedes publicar algún código de ejemplo, por favor?


Eche un vistazo al ScheduledExecutorService :

Aquí hay una clase con un método que configura un ScheduledExecutorService para que suene cada diez segundos durante una hora:

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


Prueba esto. Repetirá la función run () cada minuto establecido. Para cambiar los minutos establecidos, cambie la variable MINUTOS.

int MINUTES = 10; // The delay in minutes Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { // Function runs every MINUTES minutes. // Run the code you want here CLASSB.funcb(); // If the function you wanted was static } }, 0, 1000 * 60 * MINUTES); // 1000 milliseconds in a second * 60 per minute * the MINUTES variable.

¡No olvides hacer las importaciones!

import java.util.Timer; import java.util.TimerTask;

Para mas información, ve aquí:

http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html


import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class ClassExecutingTask { long delay = 10 * 1000; // delay in milliseconds LoopTask task = new LoopTask(); Timer timer = new Timer("TaskName"); public void start() { timer.cancel(); timer = new Timer("TaskName"); Date executionDate = new Date(); // no params = now timer.scheduleAtFixedRate(task, executionDate, delay); } private class LoopTask extends TimerTask { public void run() { System.out.println("This message will print every 10 seconds."); } } public static void main(String[] args) { ClassExecutingTask executingTask = new ClassExecutingTask(); executingTask.start(); } }


public class datetime { public String CurrentDate() { java.util.Date dt = new java.util.Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(dt); return currentTime; } public static void main(String[] args) { class SayHello extends TimerTask { datetime thisObj = new datetime(); public void run() { String todaysdate = thisObj.CurrentDate(); System.out.println(todaysdate); } } Timer timer = new Timer(); timer.schedule(new SayHello(), 0, 5000); } }