When a Java program starts, a thread runs immediately. The thread is usually called the main thread of the program because it is executed at the beginning of the program. The importance of the main thread is reflected in two aspects:
Although the main thread is automatically created when the program starts, it can be controlled by a Thread object. To do this, you have to call the method currentThread() to get a reference to it, which is a public static member of the Thread class. Its usual form is as follows:
static Thread currentThread( )
This method returns a reference to the thread that called it. Once you get a reference to the main thread, you can control the main thread like you do to control other threads.
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, the reference to the current thread (or the main thread) is obtained by calling currentThread(), which is stored in the local variable t. The program then displays the thread's information. Then the program calls setName() to change the internal name of the thread. The thread information is displayed again. Then, a cycle number begins to decrement from 5, pausing for one second each time. Pause is done by the sleep() method. The Sleep() statement clearly stipulates that the delay time is 1 millisecond. Pay attention to the try/catch block outside the loop.
The sleep() method of the Thread class may raise an InterruptedException exception. This situation will occur when other threads want to disturb the sleeping thread. This example simply prints the message whether it was interrupted. In actual programs, you must be flexible in dealing with such problems. The following is the output of this program:
Current thread: Thread[main,5,main]After name change: Thread[My Thread,5,main]54321
Note that t is used as the output when the parameter is used 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 value, and main is also 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 set. This process is handled by a proprietary runtime environment, so I will not go into details here. After the thread name is changed, t is output again. This time, the new thread name is displayed.
Let's study the methods defined by the Thread class in the program more carefully. The sleep() method indicates that the thread is called to suspend according to the millisecond time. Its usual form is as follows:
static void sleep(long million seconds) throws InterruptedException
The time to hang is clearly defined as milliseconds. This method may throw an InterruptedException exception.
There is a second form of the sleep() method, which is shown below, which allows you to specify whether the time is in milliseconds or nanoseconds as cycles.
static void sleep(long millionseconds, int nanoseconds) throws InterruptedException
The second form is only available if the period of time is allowed in nanoseconds. As shown in the above program, you can use setName() to set the thread name and use getName() to get the thread name (this process 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 specifically refers to the thread name.