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






Thread: stop

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