tiempo segundos metodo example esperar ejemplo ejecutar cronometro codigo cierto cada java concurrency

segundos - ¿Cómo llamo a algún método de bloqueo con un tiempo de espera en Java?



timer java segundos (8)

Necesita una implementación de interruptor automático como la presente en el proyecto de failsafe en GitHub.

¿Existe una buena forma estándar de llamar a un método de bloqueo con un tiempo de espera en Java? Quiero ser capaz de hacer:

// call something.blockingMethod(); // if it hasn''t come back within 2 seconds, forget it

Si eso tiene sentido.

Gracias.


Prueba esto. Solución más simple. Garantiza que si el bloque no se ejecutó dentro del límite de tiempo. el proceso terminará y arroja una excepción.

public class TimeoutBlock { private final long timeoutMilliSeconds; private long timeoutInteval=100; public TimeoutBlock(long timeoutMilliSeconds){ this.timeoutMilliSeconds=timeoutMilliSeconds; } public void addBlock(Runnable runnable) throws Throwable{ long collectIntervals=0; Thread timeoutWorker=new Thread(runnable); timeoutWorker.start(); do{ if(collectIntervals>=this.timeoutMilliSeconds){ timeoutWorker.stop(); throw new Exception("<<<<<<<<<<****>>>>>>>>>>> Timeout Block Execution Time Exceeded In "+timeoutMilliSeconds+" Milli Seconds. Thread Block Terminated."); } collectIntervals+=timeoutInteval; Thread.sleep(timeoutInteval); }while(timeoutWorker.isAlive()); System.out.println("<<<<<<<<<<####>>>>>>>>>>> Timeout Block Executed Within "+collectIntervals+" Milli Seconds."); } /** * @return the timeoutInteval */ public long getTimeoutInteval() { return timeoutInteval; } /** * @param timeoutInteval the timeoutInteval to set */ public void setTimeoutInteval(long timeoutInteval) { this.timeoutInteval = timeoutInteval; } }

ejemplo:

try { TimeoutBlock timeoutBlock = new TimeoutBlock(10 * 60 * 1000);//set timeout in milliseconds Runnable block=new Runnable() { @Override public void run() { //TO DO write block of code } }; timeoutBlock.addBlock(block);// execute the runnable block } catch (Throwable e) { //catch the exception here . Which is block didn''t execute within the time limit }



Puedes usar un Ejecutor:

ExecutorService executor = Executors.newCachedThreadPool(); Callable<Object> task = new Callable<Object>() { public Object call() { return something.blockingMethod(); } }; Future<Object> future = executor.submit(task); try { Object result = future.get(5, TimeUnit.SECONDS); } catch (TimeoutException ex) { // handle the timeout } catch (InterruptedException e) { // handle the interrupts } catch (ExecutionException e) { // handle other exceptions } finally { future.cancel(true); // may or may not desire this }

Si el future.get no regresa en 5 segundos, arroja una TimeoutException . El tiempo de espera se puede configurar en segundos, minutos, milisegundos o cualquier unidad disponible como una constante en TimeUnit .

Vea el JavaDoc para más detalles.


Supongamos que blockingMethod simplemente duerme para algunos millis:

public void blockingMethod(Object input) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } }

Mi solución es usar wait() y synchronized así:

public void blockingMethod(final Object input, long millis) { final Object lock = new Object(); new Thread(new Runnable() { @Override public void run() { blockingMethod(input); synchronized (lock) { lock.notify(); } } }).start(); synchronized (lock) { try { // Wait for specific millis and release the lock. // If blockingMethod is done during waiting time, it will wake // me up and give me the lock, and I will finish directly. // Otherwise, when the waiting time is over and the // blockingMethod is still // running, I will reacquire the lock and finish. lock.wait(millis); } catch (InterruptedException e) { e.printStackTrace(); } } }

Entonces puedes reemplazar

something.blockingMethod(input)

a

something.blockingMethod(input, 2000)

Espero eso ayude.


También hay una solución AspectJ para eso con la jcabi-aspects .

@Timeable(limit = 30, unit = TimeUnit.MINUTES) public Soup cookSoup() { // Cook soup, but for no more than 30 minutes (throw and exception if it takes any longer }

No puede ser más sucinto, pero tienes que depender de AspectJ e introducirlo en tu ciclo de construcción, por supuesto.

Hay un artículo que lo explica más: Limite el tiempo de ejecución del método Java


Ver también TimeLimiter de Guava que utiliza un Executor detrás de las escenas.


Thread thread = new Thread(new Runnable() { public void run() { something.blockingMethod(); } }); thread.start(); thread.join(2000); if (thread.isAlive()) { thread.stop(); }

Tenga en cuenta que esa parada está en desuso, mejor alternativa es establecer algún indicador booleano volátil, dentro de blockingMethod () verificarlo y salir, así:

import org.junit.*; import java.util.*; import junit.framework.TestCase; public class ThreadTest extends TestCase { static class Something implements Runnable { private volatile boolean stopRequested; private final int steps; private final long waitPerStep; public Something(int steps, long waitPerStep) { this.steps = steps; this.waitPerStep = waitPerStep; } @Override public void run() { blockingMethod(); } public void blockingMethod() { try { for (int i = 0; i < steps && !stopRequested; i++) { doALittleBit(); } } catch (InterruptedException e) { throw new RuntimeException(e); } } public void doALittleBit() throws InterruptedException { Thread.sleep(waitPerStep); } public void setStopRequested(boolean stopRequested) { this.stopRequested = stopRequested; } } @Test public void test() throws InterruptedException { final Something somethingRunnable = new Something(5, 1000); Thread thread = new Thread(somethingRunnable); thread.start(); thread.join(2000); if (thread.isAlive()) { somethingRunnable.setStopRequested(true); thread.join(2000); assertFalse(thread.isAlive()); } else { fail("Exptected to be alive (5 * 1000 > 2000)"); } } }