Skip to content

Instantly share code, notes, and snippets.

@nddipiazza
Last active November 8, 2017 05:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nddipiazza/2ba54f5c09f2f51b50d10e4d0555f394 to your computer and use it in GitHub Desktop.
Save nddipiazza/2ba54f5c09f2f51b50d10e4d0555f394 to your computer and use it in GitHub Desktop.
easier to see example of this
package test;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestTimeout {
@Test
public void testTimeout() throws Exception {
try {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
final Future<Integer> handler = executor.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
Thread.sleep(8000L);
return 5;
}
});
executor.schedule((Runnable) () -> handler.cancel(true),
5000,
TimeUnit.MILLISECONDS);
handler.get();
} catch (Exception e) {
return;
}
Assert.fail("Should have timed out");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment