List of usage examples for java.util.concurrent ThreadFactory ThreadFactory
ThreadFactory
From source file:Main.java
public static void main(String[] args) { final ThreadGroup threadGroup = new ThreadGroup("workers"); ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() { public Thread newThread(Runnable r) { return new Thread(threadGroup, r); }//from w ww . j a v a 2s .c o m }); System.out.println(threadGroup.activeCount()); }
From source file:com.btoddb.fastpersitentqueue.speedtest.SpeedTest.java
public static void main(String[] args) throws Exception { if (0 == args.length) { System.out.println();/*from w w w . j a va2 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
static ThreadFactory threadFactory(final String name, final boolean daemon) { return new ThreadFactory() { @Override/*from w w w . j a va2s . c om*/ public Thread newThread(Runnable runnable) { Thread result = new Thread(runnable, name); result.setDaemon(daemon); return result; } }; }
From source file:Main.java
public static ThreadFactory threadFactory(final String name, final boolean daemon) { return new ThreadFactory() { @Override/* ww w . ja v a2 s . c om*/ public Thread newThread(Runnable runnable) { Thread result = new Thread(runnable, name); result.setDaemon(daemon); return result; } }; }
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/*ww w. java2 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++); }/*from w w w . j a va 2s.c om*/ }); }
From source file:Main.java
/** * New thread creation factory//from www .ja v a2 s . c om * @param name name of thread * @param daemon if its daemon or not. * @return ThreadFactory instance initialized for given values. */ public static ThreadFactory threadFactory(final String name, final boolean daemon) { return new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = new Thread(r, name); thread.setDaemon(daemon); return thread; } }; }
From source file:Main.java
public static ThreadFactory daemonThreadFactory(final String name) { return new ThreadFactory() { @Override// w w w .j av a 2 s . c o m public Thread newThread(Runnable runnable) { Thread result = new Thread(runnable, name); result.setDaemon(true); return result; } }; }
From source file:Main.java
public static ThreadFactory threadFactory(final String alias) { return new ThreadFactory() { private final AtomicInteger threadCount = new AtomicInteger(); @Override// w ww . j ava2 s. co m public Thread newThread(Runnable r) { return new Thread(r, "Ehcache [" + alias + "]-" + threadCount.getAndIncrement()); } }; }
From source file:Main.java
public static ThreadFactory createThreadFactory(final String prefix) { return new ThreadFactory() { private AtomicInteger size = new AtomicInteger(); public Thread newThread(Runnable r) { Thread thread = new Thread(r); thread.setName(prefix + size.incrementAndGet()); if (thread.isDaemon()) { thread.setDaemon(false); }/* www . j a va 2s . co m*/ return thread; } }; }