Skip to content

Instantly share code, notes, and snippets.

@tadas-subonis
tadas-subonis / java reddit links
Created March 16, 2017 16:48
Links found in Java Reddit Community
PLAY_VERSION=2.1.1
wget -q http://downloads.typesafe.com/play/${PLAY_VERSION}/play-${PLAY_VERSION}.zip
unzip -q play-${PLAY_VERSION}.zip
play-${PLAY_VERSION}/play clean-all
play-${PLAY_VERSION}/play compile
play-${PLAY_VERSION}/play test
import java.util.concurrent.*;
import org.testng.annotations.*;
import static org.testng.Assert.*;
public class CoreThreadTimeOut {
final int threadPoolSize = 10;
final int timeoutMillis = 30;
private ThreadPoolExecutor threadPoolExecutor;
@BeforeMethod
void setUp() {
threadPoolExecutor = prepareThreadPoolExecutor();
}
@AfterMethod
void tearDown() throws InterruptedException {
threadPoolExecutor.shutdown();
@Test
public void threadsShouldBePresentAfterTimeoutIfWeDontAllowThreadTimeout() throws Throwable {
threadPoolExecutor.allowCoreThreadTimeOut(false);
CountDownLatch untilAllThreadsDone = startaBunchOfThreads();
untilAllThreadsDone.await();
waitUntilTimeoutOccurs();
@Test
public void threadsShouldBePresentBeforeKeepAliveTimeKicks() throws Throwable {
CountDownLatch untilAllThreadsDone = startaBunchOfThreads();
untilAllThreadsDone.await();
waitBeforeTimeoutOccurs();
assertEquals(countExecutorThreads(), threadPoolSize);
}
@Test
public void shouldRemoveThreadsAfterCoreThreadTimeoutPeriod() throws Throwable {
CountDownLatch untilAllThreadsDone = startaBunchOfThreads();
untilAllThreadsDone.await();
waitUntilTimeoutOccurs();
assertEquals(countExecutorThreads(), 0);
}
@Test
void shouldRemoveThreadsAfterCoreThreadTimeoutPeriod() throws Throwable {
ThreadPoolExecutor tpe = prepareTestThreadPool(threadCount, timeoutMillis);
long t0 = System.nanoTime();
for (int i = 0; i < threadCount; i++)
tpe.submit(new Runnable() { public void run() {}});
int count = countExecutorThreads();
if (millisElapsedSince(t0) < timeoutMillis)
void test(String[] args) throws Throwable {
/*
* Prepare
*/
ThreadPoolExecutor tpe = prepareTestThreadPool(threadCount, timeoutMillis);
long t0 = System.nanoTime();
for (int i = 0; i < threadCount; i++)
tpe.submit(new Runnable() { public void run() {}});