Search in sources :

Example 76 with ThreadFactory

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();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) Statement(java.sql.Statement) Connection(java.sql.Connection) Properties(java.util.Properties) ExecutorService(java.util.concurrent.ExecutorService) ResultSet(java.sql.ResultSet) Future(java.util.concurrent.Future) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 77 with ThreadFactory

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;
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Example 78 with ThreadFactory

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();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Example 79 with ThreadFactory

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();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) LoggingThreadGroup(org.apache.geode.internal.logging.LoggingThreadGroup)

Example 80 with ThreadFactory

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;
}
Also used : IndexInfoService(org.apache.jackrabbit.oak.plugins.index.IndexInfoService) PropertyIndexInfoProvider(org.apache.jackrabbit.oak.plugins.index.property.PropertyIndexInfoProvider) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) IndexInfoServiceImpl(org.apache.jackrabbit.oak.plugins.index.IndexInfoServiceImpl) Mounts(org.apache.jackrabbit.oak.spi.mount.Mounts) LoggerFactory(org.slf4j.LoggerFactory) IndexPathService(org.apache.jackrabbit.oak.plugins.index.IndexPathService) ImmutableList(com.google.common.collect.ImmutableList) MountInfoProvider(org.apache.jackrabbit.oak.spi.mount.MountInfoProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closer(com.google.common.io.Closer) ThreadFactory(java.util.concurrent.ThreadFactory) IndexPathServiceImpl(org.apache.jackrabbit.oak.plugins.index.IndexPathServiceImpl) Nonnull(javax.annotation.Nonnull) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) AsyncIndexInfoService(org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService) LuceneIndexInfoProvider(org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexInfoProvider) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) IOException(java.io.IOException) IndexDefinitionPrinter(org.apache.jackrabbit.oak.plugins.index.inventory.IndexDefinitionPrinter) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IndexPrinter(org.apache.jackrabbit.oak.plugins.index.inventory.IndexPrinter) BlobStore(org.apache.jackrabbit.oak.spi.blob.BlobStore) ExecutorCloser(org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser) Closeable(java.io.Closeable) AsyncIndexInfoServiceImpl(org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoServiceImpl) StatisticsProvider(org.apache.jackrabbit.oak.stats.StatisticsProvider) ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

ThreadFactory (java.util.concurrent.ThreadFactory)250 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)47 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)46 ExecutorService (java.util.concurrent.ExecutorService)45 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)21 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)19 Test (org.junit.Test)17 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)16 Future (java.util.concurrent.Future)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)13 ArrayList (java.util.ArrayList)13 LoggingThreadGroup (org.apache.geode.internal.logging.LoggingThreadGroup)12 IOException (java.io.IOException)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 ExecutionException (java.util.concurrent.ExecutionException)9 Executor (java.util.concurrent.Executor)9 ChannelFuture (io.netty.channel.ChannelFuture)8 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)8 SynchronousQueue (java.util.concurrent.SynchronousQueue)7