Thread.MAX_PRIORITY : Thread « java.lang « Java by API






Thread.MAX_PRIORITY

  
/*
 * Output:
 * 
    29465957 vs. -1589812543

 *   
 * 
 *  
 */

class MyThread implements Runnable {
  public int click = 0;

  private Thread t;

  private boolean running = true;

  public MyThread(int p) {
    t = new Thread(this);
    t.setPriority(p);
  }

  public void run() {
    while (running) {
      click++;
    }
  }

  public void stop() {
    running = false;
  }

  public void start() {
    t.start();
  }
}

public class MainClass {
  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();
    hi.start();

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

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








Related examples in the same category

1.Thread.NORM_PRIORITY
2.new Thread(Runnable target, String name)
3.Thread.activeCount()
4.Thread.currentThread()
5.Thread.dumpStack()
6.Thread.enumerate(Thread[] tarray)
7.Thread: getStackTrace()
8.Thread: getThreadGroup()
9.Thread: getUncaughtExceptionHandler()
10.Thread: interrupt()
11.Thread: isAlive()
12.Thread: isDaemon()
13.Thread: join() (Using join() to wait for threads to finish)
14.Thread: run()
15.Thread: setDaemon(boolean b)
16.Thread: setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
17.Threah.sleep(long millis)
18.Thread: setDaemon(boolean on)
19.Thread: setName(String name)
20.Thread: setPriority(int newPriority)
21.Thread: setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
22.Thread: start
23.Thread: stop
24.implements UncaughtExceptionHandler