Example usage for java.lang Thread Thread

List of usage examples for java.lang Thread Thread

Introduction

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

Prototype

public Thread(Runnable target, String name) 

Source Link

Document

Allocates a new Thread object.

Usage

From source file:Main.java

public static Thread runAsync(Runnable runnable, String threadName, boolean daemon) {
    Thread thread = new Thread(runnable, threadName);
    thread.setDaemon(daemon);//from ww  w.j  a va 2s.c  o m
    thread.start();
    return thread;
}

From source file:Main.java

static ThreadFactory threadFactory(final String name, final boolean daemon) {
    return new ThreadFactory() {
        @Override// ww w  .ja v a 2 s .co m
        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// w  ww .  j  a  v a  2 s  .  c o  m
        public Thread newThread(Runnable runnable) {
            Thread result = new Thread(runnable, name);
            result.setDaemon(daemon);
            return result;
        }
    };
}

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 av a2s .c  o m*/
    });
}

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  ww  w. j  av  a2  s  .  c o m*/
        public Thread newThread(Runnable r) {
            return new Thread(r, name + "_" + String.valueOf(i++));
        }
    });
}

From source file:Main.java

/**
 * New thread creation factory//  ww w.  j a va 2s .  co m
 * @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:JoinDemo.java

public static Thread createThread(String name, long napTime) {
    final long sleepTime = napTime;

    Runnable r = new Runnable() {
        public void run() {
            try {
                print("in run() - entering");
                Thread.sleep(sleepTime);
            } catch (InterruptedException x) {
                print("interrupted!");
            } finally {
                print("in run() - leaving");
            }//from w w  w  . j av  a  2  s . c  o  m
        }
    };

    Thread t = new Thread(r, name);
    t.start();

    return t;
}

From source file:Main.java

public static void runInNewThread(String threadName, Runnable target) throws Throwable {
    FutureTask<Object> future = new FutureTask<Object>(target, null);
    new Thread(future, threadName).start();
    try {/*from   www .j a v  a 2s  .c  o  m*/
        future.get();
    } catch (ExecutionException e) {
        throw e.getCause();
    }
}

From source file:Main.java

/**
 * Runs a thread asynchronously.// w w  w  .j  a v  a 2  s.c o  m
 *
 * @param thread The thread (Runnable) to run.
 * @param name   The name of the thread.
 */
public static void startThread(Runnable thread, String name) {

    //Adds the desired thread to the list and starts it
    Thread newThread;

    if (name != null)
        newThread = new Thread(thread, name);
    else
        newThread = new Thread(thread);

    threads.add(newThread);
    newThread.start();

}

From source file:TransitionDetectorMain.java

private static Thread startTrueWaiter(final TransitionDetector td, String name) {

    Runnable r = new Runnable() {
        public void run() {
            try {
                while (true) {
                    print("about to wait for false-to-" + "true transition, td=" + td);

                    td.waitForFalseToTrueTransition();

                    print("just noticed for false-to-" + "true transition, td=" + td);
                }/* www.j av a 2  s . c  o m*/
            } catch (InterruptedException ix) {
                return;
            }
        }
    };

    Thread t = new Thread(r, name);
    t.start();

    return t;
}