Example usage for java.util.concurrent Executors newFixedThreadPool

List of usage examples for java.util.concurrent Executors newFixedThreadPool

Introduction

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

Prototype

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) 

Source Link

Document

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue, using the provided ThreadFactory to create new threads when needed.

Usage

From source file:com.btoddb.fastpersitentqueue.speedtest.SpeedTest.java

public static void main(String[] args) throws Exception {
    if (0 == args.length) {
        System.out.println();/*ww w . j a v a2  s.  c  om*/
        System.out.println("ERROR: must specify the config file path/name");
        System.out.println();
        System.exit(1);
    }

    SpeedTestConfig config = SpeedTestConfig.create(args[0]);

    System.out.println(config.toString());

    File theDir = new File(config.getDirectory(), "speed-" + UUID.randomUUID().toString());
    FileUtils.forceMkdir(theDir);

    Fpq queue = config.getFpq();
    queue.setJournalDirectory(new File(theDir, "journals"));
    queue.setPagingDirectory(new File(theDir, "pages"));

    try {
        queue.init();

        //
        // start workers
        //

        AtomicLong counter = new AtomicLong();
        AtomicLong pushSum = new AtomicLong();
        AtomicLong popSum = new AtomicLong();

        long startTime = System.currentTimeMillis();

        Set<SpeedPushWorker> pushWorkers = new HashSet<SpeedPushWorker>();
        for (int i = 0; i < config.getNumberOfPushers(); i++) {
            pushWorkers.add(new SpeedPushWorker(queue, config, counter, pushSum));
        }

        Set<SpeedPopWorker> popWorkers = new HashSet<SpeedPopWorker>();
        for (int i = 0; i < config.getNumberOfPoppers(); i++) {
            popWorkers.add(new SpeedPopWorker(queue, config, popSum));
        }

        ExecutorService pusherExecSrvc = Executors.newFixedThreadPool(
                config.getNumberOfPushers() + config.getNumberOfPoppers(), new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable runnable) {
                        Thread t = new Thread(runnable);
                        t.setName("SpeedTest-Pusher");
                        return t;
                    }
                });

        ExecutorService popperExecSrvc = Executors.newFixedThreadPool(
                config.getNumberOfPushers() + config.getNumberOfPoppers(), new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable runnable) {
                        Thread t = new Thread(runnable);
                        t.setName("SpeedTest-Popper");
                        return t;
                    }
                });

        long startPushing = System.currentTimeMillis();
        for (SpeedPushWorker sw : pushWorkers) {
            pusherExecSrvc.submit(sw);
        }

        long startPopping = System.currentTimeMillis();
        for (SpeedPopWorker sw : popWorkers) {
            popperExecSrvc.submit(sw);
        }

        //
        // wait to finish
        //

        long endTime = startTime + config.getDurationOfTest() * 1000;
        long endPushing = 0;
        long displayTimer = 0;
        while (0 == endPushing || !queue.isEmpty()) {
            // display status every second
            if (1000 < (System.currentTimeMillis() - displayTimer)) {
                System.out.println(String.format("status (%ds) : journals = %d : memory segments = %d",
                        (endTime - System.currentTimeMillis()) / 1000,
                        queue.getJournalMgr().getJournalIdMap().size(),
                        queue.getMemoryMgr().getSegments().size()));
                displayTimer = System.currentTimeMillis();
            }

            pusherExecSrvc.shutdown();
            if (pusherExecSrvc.awaitTermination(100, TimeUnit.MILLISECONDS)) {
                endPushing = System.currentTimeMillis();
                // tell poppers, all pushers are finished
                for (SpeedPopWorker sw : popWorkers) {
                    sw.stopWhenQueueEmpty();
                }
            }
        }

        long endPopping = System.currentTimeMillis();

        popperExecSrvc.shutdown();
        popperExecSrvc.awaitTermination(10, TimeUnit.SECONDS);

        long numberOfPushes = 0;
        for (SpeedPushWorker sw : pushWorkers) {
            numberOfPushes += sw.getNumberOfEntries();
        }

        long numberOfPops = 0;
        for (SpeedPopWorker sw : popWorkers) {
            numberOfPops += sw.getNumberOfEntries();
        }

        long pushDuration = endPushing - startPushing;
        long popDuration = endPopping - startPopping;

        System.out.println("push - pop checksum = " + pushSum.get() + " - " + popSum.get() + " = "
                + (pushSum.get() - popSum.get()));
        System.out.println("push duration = " + pushDuration);
        System.out.println("pop duration = " + popDuration);
        System.out.println();
        System.out.println("pushed = " + numberOfPushes);
        System.out.println("popped = " + numberOfPops);
        System.out.println();
        System.out.println("push entries/sec = " + numberOfPushes / (pushDuration / 1000f));
        System.out.println("pop entries/sec = " + numberOfPops / (popDuration / 1000f));
        System.out.println();
        System.out.println("journals created = " + queue.getJournalsCreated());
        System.out.println("journals removed = " + queue.getJournalsRemoved());
    } finally {
        if (null != queue) {
            queue.shutdown();
        }
        //            FileUtils.deleteDirectory(theDir);
    }
}

From source file:Main.java

public static ExecutorService newExecutor(final String name, int num) {
    return Executors.newFixedThreadPool(num, new ThreadFactory() {
        private int i = 0;

        @Override//from   w ww. j ava 2  s .  c o  m
        public Thread newThread(Runnable r) {
            return new Thread(r, name + "_" + String.valueOf(i++));
        }
    });
}

From source file:Main.java

static ExecutorService newFixedThreadPool(int size, final String threadNamePrefix) {

    return Executors.newFixedThreadPool(size, new ThreadFactory() {

        int threadIdx = 0;

        public Thread newThread(Runnable r) {
            return new Thread(r, threadNamePrefix + threadIdx++);
        }/*  w  w  w.j  a  v a 2s .co m*/
    });
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(int concurrency, String poolName) {
    return Executors.newFixedThreadPool(concurrency, createFactory(poolName));
}

From source file:Main.java

/**
 * Wrapper over newFixedThreadPool. Thread names are formatted as prefix-ID, where ID is a
 * unique, sequentially assigned integer.
 *//*from w  w w. j av  a2s .  c  om*/
public static ThreadPoolExecutor newDaemonFixedThreadPool(int nThreads, String prefix) {
    ThreadFactory threadFactory = namedThreadFactory(prefix);
    return (ThreadPoolExecutor) Executors.newFixedThreadPool(nThreads, threadFactory);
}

From source file:Main.java

protected static synchronized ExecutorService getExecutorService() {
    return null != etsContainer ? etsContainer
            : (etsContainer = Executors.newFixedThreadPool(50, getThreadFactory()));
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(int qty, String processName) {
    return Executors.newFixedThreadPool(qty, newThreadFactory(processName));
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(String name, int numThreads) {
    return Executors.newFixedThreadPool(numThreads, newNamedThreadFactory(name));
}

From source file:com.webbfontaine.valuewebb.irms.IrmsEventBus.java

public IrmsEventBus() {
    super("IRMS EventBus", Executors.newFixedThreadPool(N_THREADS,
            new BasicThreadFactory.Builder().namingPattern("IRMS-EventBus-%d").build()));
}

From source file:com.connectsdk.core.Util.java

static void createExecutor() {
    Util.executor = Executors.newFixedThreadPool(NUM_OF_THREADS, new ThreadFactory() {
        @Override/*from   w ww .  ja  va 2 s  . c om*/
        public Thread newThread(Runnable r) {
            Thread th = new Thread(r);
            th.setName("2nd Screen BG");
            return th;
        }
    });
}