How many threads are there?

As a general rule, there are at least three threads executing in any Java program. First of all there's the main thread into which your program is running. This is the thread that includes the main() method that started your application. In an applet this will be the thread into which the applet viewer or web browser was launched.

Next, there's a low priority thread that handles garbage collection and runs finalizers.

In programs that use the AWT, there's also a screen updater thread that checks to see if anything needs to be repainted about 100 times a second.

Finally there are any threads your program has explicitly spawned. However you're program is always running in one thread or another. You're never outside the thread system. You can determine the currently executing thread with the static Thread.currentThread() method:

 public static native Thread currentThread()
For example, the following program prints the name of the primary thread of execution:

public class PrimaryThread {

  public static void main(String[] args) {

    System.out.println(Thread.currentThread());

  }

}
You use the currentThread() method to get a reference to the current thread so you can manipulate it.


Previous | Next | Top
Last Modified July 6, 1998
Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu