Example usage for java.lang Thread NORM_PRIORITY

List of usage examples for java.lang Thread NORM_PRIORITY

Introduction

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

Prototype

int NORM_PRIORITY

To view the source code for java.lang Thread NORM_PRIORITY.

Click Source Link

Document

The default priority that is assigned to a thread.

Usage

From source file:Main.java

public static void main(String[] args) {

    Thread t = Thread.currentThread();
    t.setName("java2s.com thread");
    // set thread priority to 1
    t.setPriority(Thread.NORM_PRIORITY);

    System.out.println("Thread = " + t);

    int count = Thread.activeCount();
    System.out.println("currently active threads = " + count);
}

From source file:clicker.java

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
    clicker lo = new clicker(Thread.NORM_PRIORITY - 2);

    lo.start();/*from   w w  w.jav a  2s .  c  o  m*/
    hi.start();
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        System.out.println("Main thread interrupted.");
    }

    lo.stop();
    hi.stop();

    // Wait for child threads to terminate.
    try {
        hi.t.join();
        lo.t.join();
    } catch (InterruptedException e) {
        System.out.println("InterruptedException caught");
    }

    System.out.println("Low-priority thread: " + lo.click);
    System.out.println("High-priority thread: " + hi.click);
}

From source file:MyThread.java

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    MyThread hi = new MyThread(Thread.NORM_PRIORITY + 2);
    MyThread lo = new MyThread(Thread.NORM_PRIORITY - 2);
    lo.start();//from   w w  w .ja  va  2 s.co  m
    hi.start();

    try {
        Thread.sleep(10000);
    } catch (Exception e) {
    }

    lo.stop();
    hi.stop();
    System.out.println(lo.click + " vs. " + hi.click);
}

From source file:org.lockss.laaws.mdx.server.LaawsMdxApp.java

public static void main(String[] args) {
    if (LOG.isDebugEnabled())
        LOG.debug("args = " + Arrays.toString(args));

    LaawsMdxApp laawsMdxApp;//ww  w  . j a  v  a 2  s. com

    if (!SystemUtils.isJavaVersionAtLeast(MIN_JAVA_VERSION)) {
        System.err.println("LOCKSS requires at least Java " + MIN_JAVA_VERSION + ", this is "
                + SystemUtils.JAVA_VERSION + ", exiting.");
        System.exit(Constants.EXIT_CODE_JAVA_VERSION);
    }

    StartupOptions opts = getStartupOptions(args);
    setSystemProperties();

    try {
        laawsMdxApp = new LaawsMdxApp(opts.getPropUrls(), opts.getGroupNames());
        laawsMdxApp.startDaemon();
        // raise priority after starting other threads, so we won't get
        // locked out and fail to exit when told.
        Thread.currentThread().setPriority(Thread.NORM_PRIORITY + 2);

    } catch (ResourceUnavailableException e) {
        LOG.error("Exiting because required resource is unavailable", e);
        System.exit(Constants.EXIT_CODE_RESOURCE_UNAVAILABLE);
        return; // compiler doesn't know that
                // System.exit() doesn't return
    } catch (Throwable e) {
        LOG.error("Exception thrown in main loop", e);
        System.exit(Constants.EXIT_CODE_EXCEPTION_IN_MAIN);
        return; // compiler doesn't know that
                // System.exit() doesn't return
    }
    if (CurrentConfig.getBooleanParam(PARAM_APP_EXIT_IMM, DEFAULT_APP_EXIT_IMM)) {
        try {
            laawsMdxApp.stop();
        } catch (RuntimeException e) {
            // ignore errors stopping daemon
        }
        System.exit(Constants.EXIT_CODE_NORMAL);
    }
    LOG.info("Done with LaawsMdxApp.main()");
}

From source file:Main.java

/** Get thread priority calc'd from runtime or settings.
 * /*from   w w w .  ja v a2 s .  c  om*/
 * @return threadPriority
 */
public static int getPriority() {
    return Thread.NORM_PRIORITY;
}

From source file:Main.java

/**
 * Starts the given {@link Runnable} tasks as daemons
 * //from   ww  w  .  j a v  a2 s . co m
 * @param tasks
 */
public static void startDaemon(Runnable... tasks) {
    for (Runnable task : tasks) {
        Thread thread = new Thread(task);
        thread.setDaemon(true);
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.start();
    }
}

From source file:Main.java

/** Start all given threads and wait on each of them until all are done.
 * From Stephan Preibisch's Multithreading.java class. See:
 * http://repo.or.cz/w/trakem2.git?a=blob;f=mpi/fruitfly/general/MultiThreading.java;hb=HEAD
 * @param threads /*from ww  w  . j ava 2  s  .  c  o m*/
 */
public static void startAndJoin(Thread[] threads) {
    for (int ithread = 0; ithread < threads.length; ++ithread) {
        threads[ithread].setPriority(Thread.NORM_PRIORITY);
        threads[ithread].start();
    }

    try {
        for (int ithread = 0; ithread < threads.length; ++ithread) {
            threads[ithread].join();
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
}

From source file:Main.java

/** Start all given threads and wait on each of them until all are done.
 * From Stephan Preibisch's Multithreading.java class. See:
 * http://repo.or.cz/w/trakem2.git?a=blob;f=mpi/fruitfly/general/MultiThreading.java;hb=HEAD
 * @param threads //from  www. j a v  a2 s.  co m
 */
public static void startAndJoin(Thread[] threads) {
    for (Thread thread : threads) {
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.start();
    }

    try {
        for (Thread thread : threads) {
            thread.join();
        }
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    }
}

From source file:Main.java

public ThreadGroupDemo() {

    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");
    pGroup.setMaxPriority(Thread.MAX_PRIORITY - 2);

    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");
    cGroup.setMaxPriority(Thread.NORM_PRIORITY);

    Thread t1 = new Thread(pGroup, this);
    t1.setPriority(Thread.MAX_PRIORITY);
    System.out.println("Starting " + t1.getName());
    t1.start();/*from  w ww  .  java 2 s. c  o m*/

    Thread t2 = new Thread(cGroup, this);
    t1.setPriority(Thread.MAX_PRIORITY);
    System.out.println("Starting " + t2.getName());
    t2.start();

    System.out.println("Active threads in \"" + pGroup.getName() + "\" = " + pGroup.activeCount());

}

From source file:org.openstreetmap.josm.data.cache.HostLimitQueueTest.java

private static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers, int queueLimit) {
    HostLimitQueue workQueue = new HostLimitQueue(queueLimit);
    ThreadPoolExecutor executor = new ThreadPoolExecutor(0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool
            workers, // do not this number of threads
            300, // keepalive for thread
            TimeUnit.SECONDS, workQueue, Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY));
    workQueue.setExecutor(executor);/*from   w  w w.  ja v a2 s .c  o  m*/
    return executor;
}