The main thread in Java thread programming

  • 2020-04-01 04:12:39
  • OfStack

When a Java program starts, a thread runs immediately. This thread is often called the program's main thread because it was executed when the program started. The main thread is important in two ways:

It is a thread that produces other child threads; Usually it has to finish execution last because it performs various closing actions.

Although the main Thread is created automatically when the program starts, it can be controlled by a Thread object. To do this, you must call the method currentThread() to get a reference to it, and currentThread() is the public, static member of the Thread class. It usually takes the following form:


  static Thread currentThread( )


This method returns a reference to the thread that called it. Once you have a reference to the main thread, you can control the main thread as you would any other thread.

Let's start by reviewing the following examples:


// Controlling the main Thread.
class CurrentThreadDemo {
  public static void main(String args[]) {
    Thread t = Thread.currentThread();
    System.out.println("Current thread: " + t);
    // change the name of the thread
    t.setName("My Thread");
    System.out.println("After name change: " + t);
    try {
      for(int n = 5; n > 0; n--) {
        System.out.println(n);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println("Main thread interrupted");
    }
  }
}

In this program, a reference to the currentThread(the main thread, naturally) is obtained by calling currentThread(), which is stored in the local variable t. The program then displays information about the thread. The program then calls setName() to change the internal name of the thread. Thread information is displayed again. Then, the number of loops decreases from 5, pausing for one second at a time. The pause is done by the sleep() method. The Sleep() statement specifies that the delay is 1 millisecond. Notice the try/catch block outside the loop.

The sleep() method of the Thread class might throw an InterruptedException. This can happen when another thread wants to disturb a sleeping thread. This example simply prints the message whether it is interrupted or not. In a real program, you have to be flexible with this kind of problem. The following is the output of the program:


Current thread: Thread[main,5,main]
After name change: Thread[My Thread,5,main]
5
4
3
2
1

Notice the output produced when t is used as an argument in the statement println(). The display order: thread name, priority, and group name. By default, the name of the main thread is main. Its priority is 5, which is also the default, and main is the name of the thread group to which it belongs. A thread group is a data structure that controls the state of threads as a whole collection. This process is handled by a proprietary runtime environment, which I won't go into here. After the thread name is changed, t is printed again. This time, the new thread name is displayed.

Let's take a closer look at the methods defined by the Thread class in the application. The sleep() method causes the thread to suspend from being called to being called at a millisecond time indication. It usually takes the following form:


  static void sleep(long milliseconds) throws InterruptedException


The hang time is explicitly defined as milliseconds. This method may throw InterruptedException.

There is a second form of the sleep() method, shown below, which allows you to specify whether the time is in milliseconds or nanoseconds.


  static void sleep(long milliseconds, int nanoseconds) throws InterruptedException

The second form is only available if the time period is allowed in nanoseconds. As shown in the program above, you can set the thread name with setName() and get the thread name with getName() (this procedure is not reflected in the program). These methods are members of the Thread class and are declared as follows:


  final void setName(String threadName)
  final String getName( )


Here, threadName refers specifically to the threadName.


Related articles: