How many threads are there?

As a general rule, there are at least three threads executing in any Java program. First there's the main thread in 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 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 | Cafe au Lait

Copyright 1997, 1998, 2006 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 2, 2006