Example usage for com.google.common.util.concurrent ThreadFactoryBuilder ThreadFactoryBuilder

List of usage examples for com.google.common.util.concurrent ThreadFactoryBuilder ThreadFactoryBuilder

Introduction

In this page you can find the example usage for com.google.common.util.concurrent ThreadFactoryBuilder ThreadFactoryBuilder.

Prototype

public ThreadFactoryBuilder() 

Source Link

Document

Creates a new ThreadFactory builder.

Usage

From source file:com.cloudera.oryx.als.computation.local.MakeRecommendations.java

@Override
public Object call() throws IOException {

    log.info("Starting recommendations");

    Config config = ConfigUtils.getDefaultConfig();
    final int howMany = config.getInt("model.recommend.how-many");

    final LongPrimitiveIterator it = X.keySetIterator();

    final File recommendDir = new File(modelDir, "recommend");
    IOUtils.mkdirs(recommendDir);/*  ww w . ja  v  a 2s  .c  om*/

    int numThreads = ExecutorUtils.getParallelism();
    ExecutorService executor = Executors.newFixedThreadPool(numThreads,
            new ThreadFactoryBuilder().setNameFormat("Recommend-%d").setDaemon(true).build());
    Collection<Future<Object>> futures = Lists.newArrayList();

    try {
        for (int i = 0; i < numThreads; i++) {
            final int workerNumber = i;
            futures.add(executor.submit(new Callable<Object>() {
                @Override
                public Void call() throws IOException {
                    Writer out = IOUtils.buildGZIPWriter(new File(recommendDir, workerNumber + ".csv.gz"));
                    try {
                        while (true) {
                            long userID;
                            synchronized (it) {
                                if (!it.hasNext()) {
                                    return null;
                                }
                                userID = it.nextLong();
                            }
                            float[] userFeatures = X.get(userID);
                            LongSet knownItemIDsForUser = knownItemIDs == null ? null
                                    : knownItemIDs.get(userID);
                            Iterable<NumericIDValue> recs = TopN.selectTopN(new RecommendIterator(userFeatures,
                                    Y.entrySet().iterator(), knownItemIDsForUser), howMany);
                            String userIDString = idMapping.toString(userID);
                            for (NumericIDValue rec : recs) {
                                out.write(DelimitedDataUtils.encode(userIDString,
                                        idMapping.toString(rec.getID()), Float.toString(rec.getValue())));
                                out.write('\n');
                            }
                        }
                    } finally {
                        out.close();
                    }
                }
            }));

        }

    } finally {
        ExecutorUtils.shutdownNowAndAwait(executor);
    }

    ExecutorUtils.checkExceptions(futures);

    log.info("Finished recommendations");

    return null;
}

From source file:org.apache.beam.runners.dataflow.worker.util.common.worker.ExecutionStateSampler.java

/**
 * Called to start the ExecutionStateSampler. Until the returned {@link Closeable} is closed, the
 * state sampler will periodically sample the current state of all the threads it has been asked
 * to manage.//from w  w w .ja va  2 s .  c om
 *
 * <p>
 */
public void start() {
    start(Executors.newSingleThreadExecutor(
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("state-sampler-%d").build()));
}

From source file:com.twitter.aurora.scheduler.stats.AsyncStatsModule.java

@Override
protected void configure() {
    final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder().setNameFormat("AsyncStat-%d").setDaemon(true).build());

    bind(TaskStatCalculator.class).in(Singleton.class);
    bind(CachedCounters.class).in(Singleton.class);
    bind(ResourceSlotProvider.class).to(OfferAdapter.class);
    bind(SlotSizeCounter.class).in(Singleton.class);

    bind(ScheduledExecutorService.class).annotatedWith(StatExecutor.class).toInstance(executor);
    LifecycleModule.bindStartupAction(binder(), StatUpdater.class);
}

From source file:org.kaaproject.kaa.server.admin.services.messaging.MessagingService.java

public void init() {
    String sendName = "send-message-call-runner-%d";
    sendPool = Executors.newFixedThreadPool(sendPoolSize,
            new ThreadFactoryBuilder().setNameFormat(sendName).build());
    configureMailSender();/*  ww w. j  av a  2 s .c om*/
}

From source file:com.netflix.conductor.core.execution.tasks.SystemTaskWorkerCoordinator.java

@Inject
public SystemTaskWorkerCoordinator(QueueDAO taskQueues, WorkflowExecutor executor, Configuration config) {
    this.taskQueues = taskQueues;
    this.executor = executor;
    this.config = config;
    this.unackTimeout = config.getIntProperty("workflow.system.task.worker.callback.seconds", 30);
    int threadCount = config.getIntProperty("workflow.system.task.worker.thread.count", 5);
    this.pollCount = config.getIntProperty("workflow.system.task.worker.poll.count", 5);
    this.workerQueueSize = config.getIntProperty("workflow.system.task.worker.queue.size", 100);
    this.workerQueue = new LinkedBlockingQueue<Runnable>(workerQueueSize);
    if (threadCount > 0) {
        ThreadFactory tf = new ThreadFactoryBuilder().setNameFormat("system-task-worker-%d").build();
        this.es = new ThreadPoolExecutor(threadCount, threadCount, 0L, TimeUnit.MILLISECONDS, workerQueue, tf);

        new Thread(() -> listen()).start();
        logger.info(/* w w  w  .j  ava2  s.c om*/
                "System Task Worker Initialized with {} threads and a callback time of {} second and queue size {} with pollCount {}",
                threadCount, unackTimeout, workerQueueSize, pollCount);
    } else {
        logger.info("System Task Worker DISABLED");
    }
}

From source file:org.glassfish.jersey.tests.memleaks.testleak.DaemonThreadMemoryLeakingResource.java

@POST
@Path("invoke")
public String invoke(@DefaultValue("1048576") @QueryParam("size") final int size) {

    final Future<?> future = Executors
            .newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(true).build()).submit(new Runnable() {
                @Override/*from w  w  w .j  a  v a2  s  .  c  om*/
                public void run() {
                    try {
                        System.out.println("Running a Thread!");
                        final int mbytes = size / (1024 * 1024);
                        final byte[][] bytes = new byte[mbytes][];
                        for (int i = 1; i <= mbytes; ++i) {
                            bytes[i - 1] = new byte[1024 * 1024];
                            System.out.println("Allocated: " + i + "MB!");
                        }

                        System.out.println("Memory allocated! Total: " + mbytes + "MB! Sleeping...");
                        for (int i = 0; i < 1000000; ++i) {
                            System.out.println("Thread " + Thread.currentThread() + " sleeping!");
                            Thread.sleep(10000);
                        }
                        System.out.println("Freeing: " + size + " of bytes. " + bytes);
                    } catch (InterruptedException e) {
                        throw new IllegalStateException("Thread Interrupted!", e);
                    } catch (Throwable e) {
                        e.printStackTrace();
                        throw e;
                    }

                }
            });

    System.out.println("Trying to allocate bytes from the thread itself.");
    final byte[] bytes = new byte[size];
    return "Future submitted: " + future + " bytes allocated: " + bytes;
}

From source file:com.cloudera.oryx.serving.generation.GenerationManager.java

protected GenerationManager(File appendTempDir) throws IOException {

    Config config = ConfigUtils.getDefaultConfig();

    this.instanceDir = config.getString("model.instance-dir");
    this.appendTempDir = appendTempDir;
    writesBetweenUpload = config.getLong("model.writes-between-upload");
    countdownToUpload = writesBetweenUpload;

    writeGeneration = NO_GENERATION;//  w w w .  j a v a 2s.  c  om

    executorService = Executors.newScheduledThreadPool(3, new ThreadFactoryBuilder().setDaemon(true).build());
    refreshSemaphore = new Semaphore(1);

    executorService.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            if (!executorService.isShutdown()) {
                try {
                    maybeRollAppender();
                } catch (Throwable t) {
                    log.warn("Exception while maybe rolling appender", t);
                }
            }
        }
    }, 2, 2, TimeUnit.MINUTES);
    maybeRollAppender();

    executorService.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            if (!executorService.isShutdown()) {
                try {
                    refresh();
                } catch (Throwable t) {
                    log.warn("Exception while refreshing", t);
                }
            }
        }
    }, 0, 7, TimeUnit.MINUTES); // Should be mutually prime with delay set above
}

From source file:co.cask.cdap.ExternalJavaProcessExecutor.java

@Override
protected void startUp() throws Exception {
    executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat(String.format("external-java-process-%s", className)).build());
    shutdownThread = createShutdownThread();

    List<String> command = ImmutableList.<String>builder().add(JAVA_PATH, "-cp", classPath, className)
            .addAll(args).build();/*from   ww w. ja  va  2 s .  c o  m*/
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.environment().putAll(processEnv);
    processBuilder.redirectErrorStream(true);
    process = processBuilder.start();
    LOG.info("Process {} started", className);
    executor.execute(createLogRunnable(process));
}

From source file:org.wso2.carbon.cluster.coordinator.rdbms.RDBMSCoordinationStrategy.java

public RDBMSCoordinationStrategy() {
    ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("RDBMSCoordinationStrategy-%d")
            .build();//  w w w  .ja  v a 2  s .c  o  m
    this.threadExecutor = Executors
            .newScheduledThreadPool(
                    CoordinationStrategyConfiguration.getInstance().getRdbmsConfigs()
                            .get(CoordinationPropertyNames.RDBMS_BASED_PERFORM_TASK_THREAD_COUNT),
                    namedThreadFactory);
    this.heartBeatInterval = CoordinationStrategyConfiguration.getInstance().getRdbmsConfigs()
            .get(CoordinationPropertyNames.RDBMS_BASED_COORDINATION_HEARTBEAT_INTERVAL);
    // Maximum age of a heartbeat. After this much of time, the heartbeat is considered invalid and node is
    // considered to have left the cluster.
    this.heartbeatMaxAge = heartBeatInterval * 2;
    this.localNodeId = generateRandomId();
    this.rdbmsMemberEventProcessor = new RDBMSMemberEventProcessor(localNodeId);
    this.communicationBusContext = new RDBMSCommunicationBusContextImpl();
}

From source file:io.druid.server.coordination.ChangeRequestHistory.java

public ChangeRequestHistory(int maxSize) {
    this.maxSize = maxSize;
    this.changes = new CircularBuffer(maxSize);

    this.waitingFutures = new LinkedHashMap<>();

    this.resolveWaitingFuturesRunnable = new Runnable() {
        @Override/*from w  w  w  .j  a  va  2s.  c  o  m*/
        public void run() {
            resolveWaitingFutures();
        }
    };

    this.singleThreadedExecutor = Executors.newSingleThreadExecutor(
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("SegmentChangeRequestHistory").build());
}