Example usage for java.util.concurrent ThreadFactory ThreadFactory

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

Introduction

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

Prototype

ThreadFactory

Source Link

Usage

From source file:Main.java

public static ExecutorService createEventsUnorderedDeliveryExecutor() {
    return Executors.newCachedThreadPool(new ThreadFactory() {
        private AtomicInteger cnt = new AtomicInteger(0);

        @Override/*ww w .j  a  v  a2 s .c o  m*/
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "Unordered Events Thread-" + cnt.incrementAndGet());
            t.setDaemon(true);
            // XXX perhaps set uncaught exception handler here according to resilience strategy
            return t;
        }
    });
}

From source file:Main.java

public static ExecutorService createEventsOrderedDeliveryExecutor() {
    return Executors.newSingleThreadExecutor(new ThreadFactory() {
        private AtomicInteger cnt = new AtomicInteger(0);

        @Override/*from   w  w  w.  jav a  2s  . co m*/
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "Ordered Events Thread-" + cnt.incrementAndGet());
            t.setDaemon(true);
            // XXX perhaps set uncaught exception handler here according to resilience strategy
            return t;
        }
    });
}

From source file:Main.java

/**
 * Instantiates a new single threaded executor whose thread has the specified name.
 *
 * @param threadName The name of the thread.
 * @return The executor.//from  w ww  . j  a v  a  2s  . c  o  m
 */
public static ExecutorService newSingleThreadExecutor(final String threadName) {
    return Executors.newSingleThreadExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, threadName);
        }
    });
}

From source file:Main.java

public static ScheduledExecutorService createStatisticsExecutor() {
    return Executors.newScheduledThreadPool(Integer.getInteger(ORG_EHCACHE_STATISTICS_EXECUTOR_POOL_SIZE, 1),
            new ThreadFactory() {
                private AtomicInteger cnt = new AtomicInteger(0);

                @Override/*  w  ww  .  j  a v a 2s.  c  o  m*/
                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r, "Statistics Thread-" + cnt.incrementAndGet());
                    t.setDaemon(true);
                    return t;
                }
            });
}

From source file:Main.java

/**
 * Instantiates a new single threaded scheduled executor whose thread has the specified name.
 *
 * @param threadName The name of the thread.
 * @return The executor./* w w  w . jav  a2  s .c o m*/
 */
public static ScheduledExecutorService newSingleThreadScheduledExecutor(final String threadName) {
    return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, threadName);
        }
    });
}

From source file:Main.java

public static ThreadPoolExecutor createExecutor(final String name, int count, int keepAlive,
        final boolean isDaemon) {
    ThreadPoolExecutor exe = new ThreadPoolExecutor(count, count, keepAlive, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
                private int threadNum = 1;

                public Thread newThread(Runnable r) {
                    Thread t = new Thread(r, name + (threadNum++));
                    t.setDaemon(isDaemon);
                    return t;
                }/* w  ww .  j a  v  a2s.c o m*/

            });

    // if (keepAlive > 0) {
    // // FIXME JDK 1.7 ?
    // if (SystemUtils.IS_JAVA_1_5 == false) {
    // try {
    // exe.allowCoreThreadTimeOut(true);
    // } catch(Throwable t) { }
    // }
    // }

    return exe;
}

From source file:Main.java

public static ThreadFactory createFactory(final String threadName) {
    return new ThreadFactory() {
        int sequence;

        public Thread newThread(Runnable r) {
            sequence += 1;/* ww w  . j  av a 2 s  .c om*/
            StringBuilder sb = new StringBuilder();
            sb.append('[').append(Thread.currentThread().getThreadGroup().getName()).append("] ");
            sb.append(threadName).append(" - ").append(sequence);
            return new Thread(r, sb.toString());
        }
    };
}

From source file:org.caffinitas.ohc.benchmark.BenchmarkOHC.java

public static void main(String[] args) throws Exception {
    Locale.setDefault(Locale.ENGLISH);
    Locale.setDefault(Locale.Category.FORMAT, Locale.ENGLISH);

    try {//from  w w  w.  j  a va 2 s  .  c o  m
        CommandLine cmd = parseArguments(args);

        String[] warmUp = cmd.getOptionValue(WARM_UP, "15,5").split(",");
        int warmUpSecs = Integer.parseInt(warmUp[0]);
        int coldSleepSecs = Integer.parseInt(warmUp[1]);

        int duration = Integer.parseInt(cmd.getOptionValue(DURATION, "60"));
        int cores = Runtime.getRuntime().availableProcessors();
        if (cores >= 8)
            cores -= 2;
        else if (cores > 2)
            cores--;
        int threads = Integer.parseInt(cmd.getOptionValue(THREADS, Integer.toString(cores)));
        long capacity = Long.parseLong(cmd.getOptionValue(CAPACITY, "" + (1024 * 1024 * 1024)));
        int hashTableSize = Integer.parseInt(cmd.getOptionValue(HASH_TABLE_SIZE, "0"));
        int segmentCount = Integer.parseInt(cmd.getOptionValue(SEGMENT_COUNT, "0"));
        float loadFactor = Float.parseFloat(cmd.getOptionValue(LOAD_FACTOR, "0"));
        int keyLen = Integer.parseInt(cmd.getOptionValue(KEY_LEN, "0"));
        int chunkSize = Integer.parseInt(cmd.getOptionValue(CHUNK_SIZE, "-1"));
        int fixedKeySize = Integer.parseInt(cmd.getOptionValue(FIXED_KEY_SIZE, "-1"));
        int fixedValueSize = Integer.parseInt(cmd.getOptionValue(FIXED_VALUE_SIZE, "-1"));
        int maxEntrySize = Integer.parseInt(cmd.getOptionValue(MAX_ENTRY_SIZE, "-1"));
        boolean unlocked = Boolean.parseBoolean(cmd.getOptionValue(UNLOCKED, "false"));
        HashAlgorithm hashMode = HashAlgorithm.valueOf(cmd.getOptionValue(HASH_MODE, "MURMUR3"));

        boolean bucketHistogram = Boolean.parseBoolean(cmd.getOptionValue(BUCKET_HISTOGRAM, "false"));

        double readWriteRatio = Double.parseDouble(cmd.getOptionValue(READ_WRITE_RATIO, ".5"));

        Driver[] drivers = new Driver[threads];
        Random rnd = new Random();
        String readKeyDistStr = cmd.getOptionValue(READ_KEY_DIST, DEFAULT_KEY_DIST);
        String writeKeyDistStr = cmd.getOptionValue(WRITE_KEY_DIST, DEFAULT_KEY_DIST);
        String valueSizeDistStr = cmd.getOptionValue(VALUE_SIZE_DIST, DEFAULT_VALUE_SIZE_DIST);
        DistributionFactory readKeyDist = OptionDistribution.get(readKeyDistStr);
        DistributionFactory writeKeyDist = OptionDistribution.get(writeKeyDistStr);
        DistributionFactory valueSizeDist = OptionDistribution.get(valueSizeDistStr);
        for (int i = 0; i < threads; i++) {
            drivers[i] = new Driver(readKeyDist.get(), writeKeyDist.get(), valueSizeDist.get(), readWriteRatio,
                    rnd.nextLong());
        }

        printMessage("Initializing OHC cache...");
        OHCacheBuilder<Long, byte[]> builder = OHCacheBuilder.<Long, byte[]>newBuilder()
                .keySerializer(
                        keyLen <= 0 ? BenchmarkUtils.longSerializer : new BenchmarkUtils.KeySerializer(keyLen))
                .valueSerializer(BenchmarkUtils.serializer).capacity(capacity);
        if (cmd.hasOption(LOAD_FACTOR))
            builder.loadFactor(loadFactor);
        if (cmd.hasOption(SEGMENT_COUNT))
            builder.segmentCount(segmentCount);
        if (cmd.hasOption(HASH_TABLE_SIZE))
            builder.hashTableSize(hashTableSize);
        if (cmd.hasOption(CHUNK_SIZE))
            builder.chunkSize(chunkSize);
        if (cmd.hasOption(MAX_ENTRY_SIZE))
            builder.maxEntrySize(maxEntrySize);
        if (cmd.hasOption(UNLOCKED))
            builder.unlocked(unlocked);
        if (cmd.hasOption(HASH_MODE))
            builder.hashMode(hashMode);
        if (cmd.hasOption(FIXED_KEY_SIZE))
            builder.fixedEntrySize(fixedKeySize, fixedValueSize);

        Shared.cache = builder.build();

        printMessage("Cache configuration: instance       : %s%n" + "                     hash-table-size: %d%n"
                + "                     load-factor    : %.3f%n" + "                     segments       : %d%n"
                + "                     capacity       : %d%n", Shared.cache, Shared.cache.hashTableSizes()[0],
                Shared.cache.loadFactor(), Shared.cache.segments(), Shared.cache.capacity());

        String csvFileName = cmd.getOptionValue(CSV, null);
        PrintStream csv = null;
        if (csvFileName != null) {
            File csvFile = new File(csvFileName);
            csv = new PrintStream(new FileOutputStream(csvFile));
            csv.println("# OHC benchmark - http://github.com/snazy/ohc");
            csv.println("# ");
            csv.printf("# started on %s (%s)%n", InetAddress.getLocalHost().getHostName(),
                    InetAddress.getLocalHost().getHostAddress());
            csv.println("# ");
            csv.printf("# Warum-up/sleep seconds:   %d / %d%n", warmUpSecs, coldSleepSecs);
            csv.printf("# Duration:                 %d seconds%n", duration);
            csv.printf("# Threads:                  %d%n", threads);
            csv.printf("# Capacity:                 %d bytes%n", capacity);
            csv.printf("# Read/Write Ratio:         %f%n", readWriteRatio);
            csv.printf("# Segment Count:            %d%n", segmentCount);
            csv.printf("# Hash table size:          %d%n", hashTableSize);
            csv.printf("# Load Factor:              %f%n", loadFactor);
            csv.printf("# Additional key len:       %d%n", keyLen);
            csv.printf("# Read key distribution:    '%s'%n", readKeyDistStr);
            csv.printf("# Write key distribution:   '%s'%n", writeKeyDistStr);
            csv.printf("# Value size distribution:  '%s'%n", valueSizeDistStr);
            csv.printf("# Type: %s%n", Shared.cache.getClass().getName());
            csv.println("# ");
            csv.printf("# started at %s%n", new Date());
            Properties props = System.getProperties();
            csv.printf("# java.version:             %s%n", props.get("java.version"));
            for (Map.Entry<Object, Object> e : props.entrySet()) {
                String k = (String) e.getKey();
                if (k.startsWith("org.caffinitas.ohc."))
                    csv.printf("# %s: %s%n", k, e.getValue());
            }
            csv.printf("# number of cores: %d%n", Runtime.getRuntime().availableProcessors());
            csv.println("# ");
            csv.println("\"runtime\";" + "\"r_count\";"
                    + "\"r_oneMinuteRate\";\"r_fiveMinuteRate\";\"r_fifteenMinuteRate\";\"r_meanRate\";"
                    + "\"r_snapMin\";\"r_snapMax\";\"r_snapMean\";\"r_snapStdDev\";"
                    + "\"r_snap75\";\"r_snap95\";\"r_snap98\";\"r_snap99\";\"r_snap999\";\"r_snapMedian\";"
                    + "\"w_count\";"
                    + "\"w_oneMinuteRate\";\"w_fiveMinuteRate\";\"w_fifteenMinuteRate\";\"w_meanRate\";"
                    + "\"w_snapMin\";\"w_snapMax\";\"w_snapMean\";\"w_snapStdDev\";"
                    + "\"w_snap75\";\"w_snap95\";\"w_snap98\";\"w_snap99\";\"w_snap999\";\"w_snapMedian\"");
        }

        printMessage(
                "Starting benchmark with%n" + "   threads     : %d%n" + "   warm-up-secs: %d%n"
                        + "   idle-secs   : %d%n" + "   runtime-secs: %d%n",
                threads, warmUpSecs, coldSleepSecs, duration);

        ThreadPoolExecutor main = new ThreadPoolExecutor(threads, threads, coldSleepSecs + 1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
                    volatile int threadNo;

                    public Thread newThread(Runnable r) {
                        return new Thread(r, "driver-main-" + threadNo++);
                    }
                });
        main.prestartAllCoreThreads();

        // warm up

        if (warmUpSecs > 0) {
            printMessage("Start warm-up...");
            runFor(warmUpSecs, main, drivers, bucketHistogram, csv);
            printMessage("");
            logMemoryUse();

            if (csv != null)
                csv.println("# warm up complete");
        }
        // cold sleep

        if (coldSleepSecs > 0) {
            printMessage("Warm up complete, sleep for %d seconds...", coldSleepSecs);
            Thread.sleep(coldSleepSecs * 1000L);
        }

        // benchmark

        printMessage("Start benchmark...");
        runFor(duration, main, drivers, bucketHistogram, csv);
        printMessage("");
        logMemoryUse();

        if (csv != null)
            csv.println("# benchmark complete");

        // finish

        if (csv != null)
            csv.close();

        System.exit(0);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

From source file:org.polymap.core.runtime.UnboundPoolExecutor.java

public static ExecutorService newInstance() {
    final int procs = Runtime.getRuntime().availableProcessors();
    final int maxThreads = procs * MAX_THREADS_PER_PROC;

    // thread factory
    ThreadFactory threadFactory = new ThreadFactory() {
        volatile int threadNumber = 0;

        public Thread newThread(Runnable r) {
            String prefix = "polymap-";
            Thread t = new Thread(r, prefix + threadNumber++);
            t.setDaemon(false);/*from w  ww .  ja  v  a 2s. c o  m*/
            t.setPriority(DEFAULT_THREAD_PRIORITY);
            return t;
        }
    };

    // thread pool
    ThreadPoolExecutor executor = new ThreadPoolExecutor(procs, maxThreads, 180L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>(), threadFactory);

    // rejected? -> wait and try again
    executor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        Random rand = new Random();

        public void rejectedExecution(Runnable r, ThreadPoolExecutor _executor) {
            do {
                try {
                    Thread.sleep(rand.nextInt(1000) + 100);
                } catch (InterruptedException e) {
                }
            } while (_executor.getActiveCount() >= maxThreads);

            _executor.execute(r);
        }
    });

    //executor.allowCoreThreadTimeOut( true );        
    return executor;
}

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

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