Example usage for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor

Introduction

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

Prototype

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue) 

Source Link

Document

Creates a new ThreadPoolExecutor with the given initial parameters, the default thread factory and the default rejected execution handler.

Usage

From source file:com.cloudhopper.commons.io.demo.FileServerMain.java

public static void loadFilesFromDir(String dir, int threads) {
    ThreadPoolExecutor ex = new ThreadPoolExecutor(threads, threads, 5000l, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());
    ex.prestartAllCoreThreads();/*ww w.  ja v  a2s .  c o m*/

    IdGenerator idGen = new UUIDIdGenerator();
    final FileStore store = new SimpleNIOFileStore(idGen, "/tmp/fileStore/");

    final long start = System.currentTimeMillis();
    int count = 0;

    Iterator<File> it = FileUtils.iterateFiles(new File(dir), null, true);
    while (it.hasNext()) {
        final File f = it.next();
        final int num = count++;
        Runnable job = new Runnable() {

            @Override
            public void run() {
                try {
                    RandomAccessFile randomAccessFile = new RandomAccessFile(f, "r");
                    FileChannel fileChannel = randomAccessFile.getChannel();
                    Id id = store.write(fileChannel);
                    System.out.println("(" + num + ") Stored " + f.getPath() + " as " + id.getName() + " after "
                            + (System.currentTimeMillis() - start) + "ms");
                } catch (Exception e) {
                    logger.error("", e);
                }
            }
        };
        ex.execute(job);
    }
}

From source file:MainClass.java

public SingleThreadAccess() {
    tpe = new ThreadPoolExecutor(1, 1, 50000L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
}

From source file:ome.services.scheduler.ThreadPool.java

public ThreadPool(int minThreads, int maxThreads, long msTimeout) {
    queue = new LinkedBlockingQueue<Runnable>();
    factory = null;//  w w w .  j a  v a 2s  . co m
    executor = new ThreadPoolExecutor(minThreads, maxThreads, msTimeout, TimeUnit.MILLISECONDS, queue); // factory
}

From source file:com.opengamma.language.context.DefaultGlobalContextEventHandler.java

public DefaultGlobalContextEventHandler() {
    final int cores = Runtime.getRuntime().availableProcessors();
    final ThreadPoolExecutor executor = new ThreadPoolExecutor(cores, cores, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>());
    executor.allowCoreThreadTimeOut(true);
    executor.setThreadFactory(new NamedThreadPoolFactory("S-Worker"));
    setSaturatingExecutor(executor);//  w  w w.j a  v  a  2 s.  co m
}

From source file:com.sm.store.AppSyncHandler.java

private void init() {

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(maxQueue);
    threadPools = new ThreadPoolExecutor(maxThreads, maxThreads, 30, TimeUnit.SECONDS, queue);
}

From source file:com.clican.pluto.cms.core.service.impl.IssueQueueServiceImpl.java

public void start() {
    log.info("starting issue queue pool sender ..." + this);

    int coreNumber = 3;
    if (coreNumber > threadNumber) {
        coreNumber = threadNumber;/* www . j  a  v a  2s.  co m*/
    }
    sendExecutor = new ThreadPoolExecutor(coreNumber, threadNumber, 60, TimeUnit.SECONDS, issueQueue);
}

From source file:com.tyndalehouse.step.tools.modules.ConvertXmlToOSISModule.java

private void convert() throws Exception {
    final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1024);
    final ExecutorService executorService = new ThreadPoolExecutor(3, 3, 1, TimeUnit.DAYS, queue);

    final File[] files = SOURCE_DIRECTORY.listFiles();
    for (final File f : files) {
        if (f.isDirectory()) {
            final File[] unzippedFiles = f.listFiles();
            for (final File unzipped : unzippedFiles) {
                if (unzipped.getName().endsWith(".xml")) {
                    executorService.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                convertToXml(f.getName(), unzipped);
                                LOGGER.debug("Finished [{}], [{}] remaining", f.getName(), queue.size());
                            } catch (Exception e) {
                                LOGGER.error("Failed to convert [{}]", f.getName(), e);
                            }//from   ww w. j  a v a  2s.  c  om
                        }
                    });
                    break;
                }
            }
            //                break;
        }
    }
    executorService.shutdown();
}

From source file:MdDetect.java

/**
 * Set up a ThreadPoolExecutor to process files in parallel
 *
 * @param capacity pool size//from w w w  . j  a  v  a2s.  c  o  m
 */
private static void initExecutor(int capacity) {
    executorService = new ThreadPoolExecutor(1, // min pool size
            Runtime.getRuntime().availableProcessors() * 4, // max pool shouldn't exceed cores
            2, // how many * how long?
            TimeUnit.SECONDS, // unit of time
            new ArrayBlockingQueue<Runnable>(capacity // initialize the list with a finite capacity
            ));
}

From source file:org.sbq.batch.mains.ActivityEmulator.java

private ActivityEmulator() {
    super();// ww  w. ja va  2  s.  c o  m
    blockingQueue = new LinkedBlockingQueue<Runnable>();
    executor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS, blockingQueue);
    appCtx = new AnnotationConfigApplicationContext(ActivityEmulatorConfiguration.class);
    userService = appCtx.getBean(UserService.class);
    Map<String, AtomicBoolean> writableUserStatusByLogin = new HashMap<String, AtomicBoolean>();
    for (User user : userService.findAllUsers()) {
        writableUserStatusByLogin.put(user.getLogin(), new AtomicBoolean(false));
    }
    userStatusByLogin = Collections.unmodifiableMap(writableUserStatusByLogin);
}

From source file:com.pinterest.rocksplicator.controller.WorkerPoolTest.java

@Test
public void testAssignSingleTask() throws Exception {
    Semaphore idleWorkersSemaphore = new Semaphore(0);
    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(1));
    WorkerPool workerPool = new WorkerPool(threadPoolExecutor, idleWorkersSemaphore, new TaskQueue() {
    });//w ww .  ja v a2s .  c  om
    workerPool.assignTask(getSleepIncrementTask());
    Thread.sleep(2000);
    Assert.assertEquals(1, SleepIncrementTask.executionCounter.intValue());
    workerPool.assignTask(getSleepIncrementTask());
    Thread.sleep(2000);
    Assert.assertEquals(2, SleepIncrementTask.executionCounter.intValue());
    Assert.assertEquals(2, idleWorkersSemaphore.availablePermits());
}