use of java.util.concurrent.ThreadFactory in project phoenix by apache.
the class ImmutableIndexIT method testCreateIndexWhileUpsertingData.
@Test
public void testCreateIndexWhileUpsertingData() throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
String tableName = "TBL_" + generateUniqueName();
String indexName = "IND_" + generateUniqueName();
String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName);
String fullIndexName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName);
String ddl = "CREATE TABLE " + fullTableName + TestUtil.TEST_TABLE_SCHEMA + tableDDLOptions;
String indexDDL = "CREATE " + (localIndex ? "LOCAL" : "") + " INDEX " + indexName + " ON " + fullTableName + " (long_pk, varchar_pk)" + " INCLUDE (long_col1, long_col2)";
int numThreads = 2;
ExecutorService executorService = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
t.setPriority(Thread.MIN_PRIORITY);
return t;
}
});
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
conn.setAutoCommit(true);
Statement stmt = conn.createStatement();
stmt.execute(ddl);
ResultSet rs;
rs = conn.createStatement().executeQuery("SELECT /*+ NO_INDEX */ COUNT(*) FROM " + fullTableName);
assertTrue(rs.next());
int dataTableRowCount = rs.getInt(1);
assertEquals(0, dataTableRowCount);
List<Future<?>> futureList = Lists.newArrayListWithExpectedSize(numThreads);
for (int i = 0; i < numThreads; ++i) {
futureList.add(executorService.submit(new UpsertRunnable(fullTableName)));
}
// upsert some rows before creating the index
Thread.sleep(100);
// create the index
try (Connection conn2 = DriverManager.getConnection(getUrl(), props)) {
conn2.createStatement().execute(indexDDL);
}
// upsert some rows after creating the index
Thread.sleep(50);
// cancel the running threads
stopThreads = true;
executorService.shutdown();
assertTrue(executorService.awaitTermination(30, TimeUnit.SECONDS));
rs = conn.createStatement().executeQuery("SELECT /*+ NO_INDEX */ COUNT(*) FROM " + fullTableName);
assertTrue(rs.next());
dataTableRowCount = rs.getInt(1);
rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM " + fullIndexName);
assertTrue(rs.next());
int indexTableRowCount = rs.getInt(1);
assertEquals("Data and Index table should have the same number of rows ", dataTableRowCount, indexTableRowCount);
} finally {
executorService.shutdownNow();
}
}
use of java.util.concurrent.ThreadFactory in project jackrabbit-oak by apache.
the class Oak method defaultScheduledExecutor.
/**
* Default {@code ScheduledExecutorService} used for scheduling background tasks.
* This default spawns up to 32 background thread on an as need basis. Idle
* threads are pruned after one minute.
* @return fresh ScheduledExecutorService
*/
public static ScheduledExecutorService defaultScheduledExecutor() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(32, new ThreadFactory() {
private final AtomicInteger counter = new AtomicInteger();
@Override
public Thread newThread(@Nonnull Runnable r) {
Thread thread = new Thread(r, createName());
thread.setDaemon(true);
return thread;
}
private String createName() {
return "oak-scheduled-executor-" + counter.getAndIncrement();
}
});
executor.setKeepAliveTime(1, TimeUnit.MINUTES);
executor.allowCoreThreadTimeOut(true);
return executor;
}
use of java.util.concurrent.ThreadFactory in project jackrabbit by apache.
the class DynamicPooledExecutor method startInstance.
private static synchronized void startInstance() {
instances++;
if (executor == null) {
ThreadFactory f = new ThreadFactory() {
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "DynamicPooledExecutor");
t.setDaemon(true);
return t;
}
};
executor = new ThreadPoolExecutor(1, Runtime.getRuntime().availableProcessors(), 500, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), f);
lastCheck = System.currentTimeMillis();
}
}
use of java.util.concurrent.ThreadFactory in project geode by apache.
the class WanLocatorDiscovererImpl method discover.
@Override
public void discover(int port, DistributionConfigImpl config, LocatorMembershipListener locatorListener, final String hostnameForClients) {
final LoggingThreadGroup loggingThreadGroup = LoggingThreadGroup.createThreadGroup("WAN Locator Discovery Logger Group", logger);
final ThreadFactory threadFactory = new ThreadFactory() {
public Thread newThread(final Runnable task) {
final Thread thread = new Thread(loggingThreadGroup, task, "WAN Locator Discovery Thread");
thread.setDaemon(true);
return thread;
}
};
this._executor = Executors.newCachedThreadPool(threadFactory);
exchangeLocalLocators(port, config, locatorListener, hostnameForClients);
exchangeRemoteLocators(port, config, locatorListener, hostnameForClients);
this._executor.shutdown();
}
use of java.util.concurrent.ThreadFactory in project jackrabbit-oak by apache.
the class IndexHelper method createExecutor.
private ThreadPoolExecutor createExecutor() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(0, 5, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
private final AtomicInteger counter = new AtomicInteger();
private final Thread.UncaughtExceptionHandler handler = (t, e) -> log.warn("Error occurred in asynchronous processing ", e);
@Override
public Thread newThread(@Nonnull Runnable r) {
Thread thread = new Thread(r, createName());
thread.setDaemon(true);
thread.setPriority(Thread.MIN_PRIORITY);
thread.setUncaughtExceptionHandler(handler);
return thread;
}
private String createName() {
return "oak-lucene-" + counter.getAndIncrement();
}
});
executor.setKeepAliveTime(1, TimeUnit.MINUTES);
executor.allowCoreThreadTimeOut(true);
return executor;
}
Aggregations