Example usage for java.util.concurrent ScheduledThreadPoolExecutor getKeepAliveTime

List of usage examples for java.util.concurrent ScheduledThreadPoolExecutor getKeepAliveTime

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledThreadPoolExecutor getKeepAliveTime.

Prototype

public long getKeepAliveTime(TimeUnit unit) 

Source Link

Document

Returns the thread keep-alive time, which is the amount of time that threads may remain idle before being terminated.

Usage

From source file:com.ksc.http.timers.ClientExecutionAndRequestTimerTestUtils.java

/**
 * Waits until a little after the thread pools keep alive time and then asserts that all thre
 * /* w  ww . j a  v  a2  s.  c  o  m*/
 * @param timerExecutor
 *            Executor used by timer implementation
 * @throws InterruptedException
 */
public static void assertCoreThreadsShutDownAfterBeingIdle(ScheduledThreadPoolExecutor timerExecutor) {
    try {
        Thread.sleep(timerExecutor.getKeepAliveTime(TimeUnit.MILLISECONDS) + 1000);
    } catch (InterruptedException ignored) {
    }
    assertEquals(0, timerExecutor.getPoolSize());
}

From source file:com.amazonaws.http.AmazonHttpClientRequestTimeoutTest.java

@Test
public void testRequestTimerCancelledTask() throws IOException, InterruptedException {
    ClientConfiguration config = new ClientConfiguration().withRequestTimeout(5 * 1000).withMaxErrorRetry(0);
    HttpClientFactory httpClientFactory = new HttpClientFactory();
    HttpClient rawHttpClient = spy(httpClientFactory.createHttpClient(config));

    HttpResponseProxy responseProxy = createHttpResponseProxySpy();
    doReturn(responseProxy).when(rawHttpClient).execute(any(HttpRequestBase.class), any(HttpContext.class));

    String localhostEndpoint = "http://localhost:0";
    Request<?> request = new EmptyHttpRequest(localhostEndpoint, HttpMethodName.GET);

    AmazonHttpClient httpClient = new AmazonHttpClient(config, rawHttpClient, null);

    try {/*w  w w. j  a  v  a2 s. co m*/
        httpClient.execute(request, new NullResponseHandler(), new NullErrorResponseHandler(),
                new ExecutionContext());
        Assert.fail("Should have been unable to unmarshall the response!");
    } catch (AmazonClientException e) {
        Assert.assertTrue(e.getCause() instanceof RuntimeException);
        RuntimeException re = (RuntimeException) e.getCause();
        Assert.assertTrue(re.getMessage().contains("Unable to unmarshall response"));
    }

    /* Verify the response was buffered when enforcing the request timeout. */
    verify(responseProxy).setEntity(any(BufferedHttpEntity.class));

    /* Verify cancelled tasks are removed on demand and the core threads die out after the keep alive time. */
    ScheduledThreadPoolExecutor httpRequestTimer = httpClient.getHttpRequestTimer().getExecutor();
    Assert.assertEquals(0, httpRequestTimer.getCompletedTaskCount());
    Assert.assertEquals(0, httpRequestTimer.getQueue().size());
    Assert.assertEquals(1, httpRequestTimer.getPoolSize());
    Thread.sleep(httpRequestTimer.getKeepAliveTime(TimeUnit.MILLISECONDS) + 1000);
    Assert.assertEquals(0, httpRequestTimer.getPoolSize());

    httpClient.shutdown();
}