guava
guava copied to clipboard
TimeLimiter.callWithTimeout Not working
While using TimeLimiter.callWithTimeout(Callable
TimeLimiter stl = SimpleTimeLimiter.create(callTimeoutPool);
stl.callWithTimeout(new Callable<Object>() {
@Override
public Object call()
{
//Logic here
}
}, callTimeout, TimeUnit.MILLISECONDS);
}
I would recommend asking for help on Stack Overflow and providing a more complete example. If it does emerge that there's a plausible defect in the library please reopen with the specific code that fails. Thanks!
Sample code
Long callTimeout = 1L;
ExecutorService callTimeoutPool= Executors.newSingleThreadExecutor( new ThreadFactoryBuilder().setNameFormat("name").setDaemon(true).build());
LOGGER.log(Level.INFO, "[ TimeoutImpl : timedCall ] currentThread name = {0}", new Object[]{Thread.currentThread().getName()});
try
{
new SimpleTimeLimiter().callWithTimeout(new Callable(){
@Override
public Object call()
{
int i=0;
while(true)
{
System.out.println(i++);
if(false)
break;
}
return true;
}
}, callTimeout, TimeUnit.MILLISECONDS, true);
}
catch(InterruptedException e)
{
LOGGER.log(Level.SEVERE, "[ TimeoutImpl : timedCall ] InterruptedException" ,e );
Thread.currentThread().interrupt();
callTimeoutPool.shutdownNow();
}
catch(Exception e)
{
LOGGER.log(Level.SEVERE, "[ TimeoutImpl : timedCall ] Exception" ,e );
Thread.currentThread().interrupt();
callTimeoutPool.shutdownNow();
}
}
please correct me what i did wrong on this implementation @kevinb9n