use of java.util.concurrent.ThreadPoolExecutor in project newts by OpenNMS.
the class ImportRunner method parMap.
private Observable<Boolean> parMap(Observable<List<Sample>> samples, MetricRegistry metrics, Func1<List<Sample>, Boolean> insert) {
final Timer waitTime = metrics.timer("wait-time");
@SuppressWarnings("serial") final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(m_maxThreadQueueSize == 0 ? m_threadCount * 3 : m_maxThreadQueueSize) {
@Override
public boolean offer(Runnable r) {
try (Context time = waitTime.time()) {
this.put(r);
return true;
} catch (InterruptedException e) {
throw Exceptions.propagate(e);
}
}
@Override
public boolean add(Runnable r) {
try (Context time = waitTime.time()) {
this.put(r);
return true;
} catch (InterruptedException e) {
throw Exceptions.propagate(e);
}
}
};
final ThreadPoolExecutor executor = new ThreadPoolExecutor(m_threadCount, m_threadCount, 0L, TimeUnit.MILLISECONDS, workQueue);
metrics.register("active-threads", new Gauge<Integer>() {
@Override
public Integer getValue() {
return executor.getActiveCount();
}
});
metrics.register("pool-size", new Gauge<Integer>() {
@Override
public Integer getValue() {
return executor.getPoolSize();
}
});
metrics.register("largest-pool-size", new Gauge<Integer>() {
@Override
public Integer getValue() {
return executor.getLargestPoolSize();
}
});
metrics.register("work-queue-size", new Gauge<Integer>() {
@Override
public Integer getValue() {
return workQueue.size();
}
});
return parMap(samples, executor, metrics, insert);
}
use of java.util.concurrent.ThreadPoolExecutor in project Signal-Android by WhisperSystems.
the class ThreadUtil method newDynamicSingleThreadedExecutor.
public static ExecutorService newDynamicSingleThreadedExecutor() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
executor.allowCoreThreadTimeOut(true);
return executor;
}
use of java.util.concurrent.ThreadPoolExecutor in project pinot by linkedin.
the class CompositeFutureTest method testMultiFutureComposite3.
@Test
public /**
* Cancelled Future. Future Client calls get() and another listens before cancel().
* A response and exception arrives after cancel but they should be discarded.
*/
void testMultiFutureComposite3() throws Exception {
List<String> keys = new ArrayList<String>();
int numFutures = 100;
int numSuccessFutures = 50;
Map<String, KeyedFuture<String, String>> futureMap = new HashMap<String, KeyedFuture<String, String>>();
for (int i = 0; i < numFutures; i++) {
String key = "key_" + i;
keys.add(key);
AsyncResponseFuture<String, String> future = new AsyncResponseFuture<String, String>(key, "");
futureMap.put(key, future);
}
CompositeFuture<String, String> compositeFuture = new CompositeFuture<String, String>("a", GatherModeOnError.AND);
compositeFuture.start(futureMap.values());
ResponseCompositeFutureClientRunnerListener runner = new ResponseCompositeFutureClientRunnerListener(compositeFuture);
ResponseCompositeFutureClientRunnerListener listener = new ResponseCompositeFutureClientRunnerListener(compositeFuture);
compositeFuture.addListener(listener, null);
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
executor.execute(runner);
// No guarantees as this only ensures the thread is started but not blocking in get().
runner.waitForAboutToGet();
Thread.sleep(100);
compositeFuture.cancel(false);
// Send response for some of the underlying futures
for (int i = 0; i < numSuccessFutures; i++) {
String message = "dummy Message_" + i;
String k = "key_" + i;
AsyncResponseFuture<String, String> future = (AsyncResponseFuture<String, String>) futureMap.get(k);
future.onSuccess(message);
}
// Send exception for some of the underlying futures
for (int i = numSuccessFutures; i < numFutures; i++) {
Exception expectedError = new Exception("error processing_" + i);
String k = "key_" + i;
AsyncResponseFuture<String, String> future = (AsyncResponseFuture<String, String>) futureMap.get(k);
future.onError(expectedError);
}
runner.waitForDone();
Assert.assertTrue(runner.isCancelled(), "Composite Cancelled ?");
Assert.assertTrue(runner.isDone(), "Composite Is Done ? ");
Assert.assertTrue(runner.getMessage().isEmpty(), "Composite No Reponse :");
Assert.assertTrue(runner.getError().isEmpty(), "Composite No Error :");
for (int i = 0; i < numFutures; i++) {
String k = "key_" + i;
AsyncResponseFuture<String, String> future = (AsyncResponseFuture<String, String>) futureMap.get(k);
Assert.assertTrue(future.isCancelled(), "Cancelled ?");
Assert.assertTrue(future.isDone(), "Is Done ? ");
Assert.assertNull(future.get(), "No Reponse :");
Assert.assertNull(future.getError(), "No Error :");
}
Assert.assertTrue(listener.isCancelled(), "listener Cancelled ?");
Assert.assertTrue(listener.isDone(), "listener Is Done ? ");
Assert.assertTrue(listener.getMessage().isEmpty(), "listener No Reponse :");
Assert.assertTrue(listener.getError().isEmpty(), "listener No Error :");
executor.shutdown();
}
use of java.util.concurrent.ThreadPoolExecutor in project pinot by linkedin.
the class SelectingFutureTest method testMultiFutureComposite3.
@Test
public /**
* 3 futures. we got errors from first future, response from second future and error from the 3rd future.
* The composite future should have second future and no error.
* @throws Exception
*/
void testMultiFutureComposite3() throws Exception {
List<String> keys = new ArrayList<String>();
int numFutures = 3;
List<KeyedFuture<String, String>> futureList = new ArrayList<KeyedFuture<String, String>>();
for (int i = 0; i < numFutures; i++) {
String key = "key_" + i;
keys.add(key);
AsyncResponseFuture<String, String> future = new AsyncResponseFuture<String, String>(key, "");
futureList.add(future);
}
SelectingFuture<String, String> compositeFuture = new SelectingFuture<String, String>("test");
compositeFuture.start(futureList);
ResponseCompositeFutureClientRunnerListener runner = new ResponseCompositeFutureClientRunnerListener(compositeFuture);
ResponseCompositeFutureClientRunnerListener listener = new ResponseCompositeFutureClientRunnerListener(compositeFuture);
compositeFuture.addListener(listener, null);
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
executor.execute(runner);
// No guarantees as this only ensures the thread is started but not blocking in get().
runner.waitForAboutToGet();
Thread.sleep(100);
String expectedMessage = null;
String message = "dummy Message_" + 0;
// First future gets an error
Throwable error = new Exception(message);
AsyncResponseFuture<String, String> future = (AsyncResponseFuture<String, String>) futureList.get(0);
future.onError(error);
//Second future gets a response
message = "dummy Message_" + 1;
future = (AsyncResponseFuture<String, String>) futureList.get(1);
future.onSuccess(message);
expectedMessage = message;
// Third future gets a response
message = "dummy Message_" + 2;
error = new Exception(message);
future = (AsyncResponseFuture<String, String>) futureList.get(2);
future.onError(error);
runner.waitForDone();
Assert.assertFalse(runner.isCancelled(), "Composite Cancelled ?");
Assert.assertTrue(runner.isDone(), "Composite Is Done ? ");
Assert.assertNull(runner.getError(), "Composite No Error :");
Assert.assertEquals(runner.getMessage(), expectedMessage, "Response");
Assert.assertFalse(futureList.get(0).isCancelled(), "First response not cancelled");
Assert.assertFalse(futureList.get(1).isCancelled(), "Second response not cancelled");
Assert.assertTrue(futureList.get(2).isCancelled(), "Third response cancelled");
Assert.assertNotNull(futureList.get(0).getError());
Assert.assertNull(futureList.get(1).getError());
Assert.assertNull(futureList.get(2).getError());
Assert.assertNull(futureList.get(0).get());
Assert.assertEquals(futureList.get(1).getOne(), runner.getMessage());
Assert.assertNull(futureList.get(2).get());
executor.shutdown();
}
use of java.util.concurrent.ThreadPoolExecutor in project pinot by linkedin.
the class KeyedPoolImplTest method testShutdownWhileCheckingOut.
@Test
public void testShutdownWhileCheckingOut() throws Exception {
ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1);
ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>());
int numKeys = 1;
int numResourcesPerKey = 1;
Map<String, List<String>> resources = buildCreateMap(numKeys, numResourcesPerKey);
BlockingTestResourceManager rm = new BlockingTestResourceManager(resources, null, null, null);
KeyedPool<String, String> kPool = new KeyedPoolImpl<String, String>(0, 1, 1000L, 1000 * 60 * 60, rm, timedExecutor, service, null);
kPool.start();
AsyncResponseFuture<String, String> f = (AsyncResponseFuture<String, String>) kPool.checkoutObject(getKey(0));
boolean isTimedout = false;
try {
f.get(2, TimeUnit.SECONDS);
} catch (TimeoutException e) {
isTimedout = true;
}
Assert.assertTrue(isTimedout);
kPool.shutdown().get();
// Future should have been done with error
Assert.assertNull(f.get());
Assert.assertNotNull(f.getError());
boolean cancelled = f.cancel(false);
Assert.assertFalse(cancelled);
Assert.assertFalse(f.isCancelled());
Assert.assertTrue(f.isDone());
rm.getCreateBlockLatch().countDown();
Thread.sleep(5000);
}
Aggregations