Example usage for java.lang Thread setName

List of usage examples for java.lang Thread setName

Introduction

In this page you can find the example usage for java.lang Thread setName.

Prototype

public final synchronized void setName(String name) 

Source Link

Document

Changes the name of this thread to be equal to the argument name .

Usage

From source file:org.kududb.client.BaseKuduTest.java

/**
 * Starts a process using the provided command and configures it to be daemon,
 * redirects the stderr to stdout, and starts a thread that will read from the process' input
 * stream and redirect that to LOG./*from   w ww  . j  av a  2s.  c  o  m*/
 * @param command Process and options
 * @return The started process
 * @throws Exception Exception if an error prevents us from starting the process,
 * or if we were able to start the process but noticed that it was then killed (in which case
 * we'll log the exit value).
 */
static Process configureAndStartProcess(String[] command) throws Exception {
    LOG.info("Starting process: {}", Joiner.on(" ").join(command));
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.redirectErrorStream(true);
    Process proc = processBuilder.start();
    ProcessInputStreamLogPrinterRunnable printer = new ProcessInputStreamLogPrinterRunnable(
            proc.getInputStream());
    Thread thread = new Thread(printer);
    thread.setDaemon(true);
    thread.setName(command[0]);
    PROCESS_INPUT_PRINTERS.add(thread);
    thread.start();

    Thread.sleep(300);
    try {
        int ev = proc.exitValue();
        throw new Exception(
                "We tried starting a process (" + command[0] + ") but it exited with " + "value=" + ev);
    } catch (IllegalThreadStateException ex) {
        // This means the process is still alive, it's like reverse psychology.
    }
    return proc;
}

From source file:password.pwm.util.Helper.java

public static ThreadFactory makePwmThreadFactory(final String namePrefix, final boolean daemon) {
    return new ThreadFactory() {
        private final ThreadFactory realThreadFactory = Executors.defaultThreadFactory();

        @Override/* w w w  . java 2  s. c om*/
        public Thread newThread(final Runnable r) {
            final Thread t = realThreadFactory.newThread(r);
            t.setDaemon(daemon);
            if (namePrefix != null) {
                final String newName = namePrefix + t.getName();
                t.setName(newName);
            }
            return t;
        }
    };
}

From source file:voldemort.utils.RebalanceUtils.java

public static ExecutorService createExecutors(int numThreads) {

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

        @Override//from w  w w.  ja v  a2 s  .  c o m
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName(r.getClass().getName());
            return thread;
        }
    });
}

From source file:it.crs4.pydoop.mapreduce.pipes.TaskLog.java

public static ScheduledExecutorService createLogSyncer() {
    final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
        @Override/*w  w w.j a v  a 2  s  . c o m*/
        public Thread newThread(Runnable r) {
            final Thread t = Executors.defaultThreadFactory().newThread(r);
            t.setDaemon(true);
            t.setName("Thread for syncLogs");
            return t;
        }
    });
    ShutdownHookManager.get().addShutdownHook(new Runnable() {
        @Override
        public void run() {
            TaskLog.syncLogsShutdown(scheduler);
        }
    }, 50);
    scheduler.scheduleWithFixedDelay(new Runnable() {
        @Override
        public void run() {
            TaskLog.syncLogs();
        }
    }, 0L, 5L, TimeUnit.SECONDS);
    return scheduler;
}

From source file:com.apptentive.android.sdk.module.engagement.interaction.InteractionManager.java

public static void asyncFetchAndStoreInteractions(final Context context) {

    if (!isPollForInteractions(context)) {
        Log.v("Interaction polling is disabled.");
        return;//from   ww  w.ja  v a2s. co  m
    }

    if (hasCacheExpired(context)) {
        Log.d("Interaction cache has expired. Fetching new interactions.");
        Thread thread = new Thread() {
            public void run() {
                fetchAndStoreInteractions(context);
            }
        };
        Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable) {
                Log.w("UncaughtException in InteractionManager.", throwable);
                MetricModule.sendError(context.getApplicationContext(), throwable, null, null);
            }
        };
        thread.setUncaughtExceptionHandler(handler);
        thread.setName("Apptentive-FetchInteractions");
        thread.start();
    } else {
        Log.d("Interaction cache has not expired. Using existing interactions.");
    }
}

From source file:com.microsoft.tfs.util.process.ProcessRunner.java

/**
 * Starts the given process runner in its own thread, returning immediately.
 * Check on the runner's state through its public methods or pass a
 * {@link ProcessFinishedHandler} to the runner on creation.
 *
 * @param runner// ww w  .ja  v a 2  s . co  m
 *        the runner to start in its own thread (not null).
 */
public static void runAsync(final ProcessRunner runner) {
    Check.notNull(runner, "runner"); //$NON-NLS-1$

    final Thread thread = new Thread(runner);
    thread.setName("Process Runner"); //$NON-NLS-1$

    /*
     * Sneak in a reference to the new thread. This must be done before
     * start() so that calls to interrupt() cause the correct exception
     * (because the runner is in the NEW state).
     */
    runner.setThread(thread);

    thread.start();
}

From source file:ee.ria.xroad.proxy.clientproxy.ClientMessageProcessor.java

private static ExecutorService createSoapHandlerExecutor() {
    return Executors.newCachedThreadPool(new ThreadFactory() {
        @Override/*  w w w.j  ava2s.co m*/
        public Thread newThread(Runnable r) {
            Thread handlerThread = new Thread(r);
            handlerThread.setName(Thread.currentThread().getName() + "-soap");

            return handlerThread;
        }
    });
}

From source file:com.tascape.qa.th.Utils.java

public static void waitForOutputLine(final Process process, String lineExpected, final long timeout)
        throws IOException {
    Thread t = new Thread() {
        @Override//from  ww w . j  av a2 s. c om
        public void run() {
            try {
                Thread.sleep(timeout);
            } catch (InterruptedException ex) {
                LOG.warn(ex.getMessage());
            } finally {
                if (process != null) {
                    process.destroy();
                }
            }
        }
    };
    t.setName(Thread.currentThread().getName() + "-" + t.hashCode());
    t.setDaemon(true);
    t.start();

    try (BufferedReader stdIn = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
        for (String line = stdIn.readLine(); line != null;) {
            if (line.contains(lineExpected)) {
                break;
            }
            line = stdIn.readLine();
        }
    }
    t.interrupt();
}

From source file:com.jhash.oimadmin.Utils.java

public static void executeAsyncOperation(String operationName, Runnable operation) {
    logger.debug("Setting up execution of {} in separate thread", operationName);
    Thread oimConnectionThread = threadFactory.newThread(new Runnable() {

        @Override//  w  w  w  .j a v a 2  s.c o m
        public void run() {
            try {
                logger.debug("Trying to run operation {}", operationName);
                operation.run();
                logger.debug("Completed operation {}.", operationName);
            } catch (Exception exception) {
                logger.warn("Failed to run operation " + operationName, exception);
            }
        }
    });
    oimConnectionThread.setDaemon(true);
    oimConnectionThread.setName(operationName);
    oimConnectionThread.start();
    logger.debug("Completed setup of execution of {} in separate thread", operationName);
}

From source file:sx.blah.discord.api.internal.DiscordUtils.java

/**
 * This creates a {@link ThreadFactory} which produces threads which run as daemons.
 *
 * @param threadName The name of threads created by the returned factory.
 * @return The new daemon thread factory.
 *//*from w  w w  . j  a  va  2 s .c o m*/
public static ThreadFactory createDaemonThreadFactory(String threadName) {
    return (runnable) -> { //Ensures all threads are daemons
        Thread thread = Executors.defaultThreadFactory().newThread(runnable);
        if (threadName != null)
            thread.setName(threadName);
        thread.setDaemon(true);
        return thread;
    };
}