2009-07-22 8 views
72

¿Existe una buena forma estándar de llamar a un método de bloqueo con un tiempo de espera en Java? Quiero poder hacer:¿Cómo llamo a algún método de bloqueo con un tiempo de espera en Java?

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

si tiene sentido.

Gracias.

+1

Como referencia, echa un vistazo a Java concurrencia en la práctica por Brian Goetz pp 126 - 134, concretamente en la sección 6.3.7 "La colocación de los límites de tiempo en las tareas" ISN –

Respuesta

123

Se podría utilizar 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 vuelve en 5 segundos, se lanza una TimeoutException. El tiempo de espera se puede configurar en segundos, minutos, milisegundos o cualquier unidad disponible como una constante en TimeUnit.

Consulte el JavaDoc para obtener más información.

+5

El método de bloqueo continuará ejecutándose incluso después del tiempo de espera, ¿verdad? –

+0

Eso depende de future.cancel. Dependiendo de lo que esté haciendo el método de bloqueo en ese momento, puede terminar o no. – skaffman

+0

probado y trabajado aquí. – Jus12

1
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 la parada está en desuso, una mejor alternativa es establecer algún indicador booleano volátil, en el interior blockingMethod() comprobarlo y salida, 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)"); 
     } 
    } 
} 
1

Véase también la guayaba de TimeLimiter que utiliza una Ejecutor detrás de las escenas.

0

Supongamos blockingMethod simplemente dormir para algunos millis:

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

Mi solución es utilizar 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(); 
     } 
    } 
} 

así que se puede sustituir

something.blockingMethod(input)

a

something.blockingMethod(input, 2000)

espero que ayude.

3

También hay una solución AspectJ para eso con la biblioteca 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 conciso, pero usted tiene que depender de AspectJ e introducirlo en el ciclo de vida de construcción, por supuesto.

Hay un artículo que explica aún más: Limit Java Method Execution Time

1

Pruebe 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 
    } 
Cuestiones relacionadas