When a Java program starts, a thread runs immediately, which is usually called the main thread of the program (main thread) because it is executed at the start of the program. The importance of the main thread is embodied in two aspects:
- It is the thread that produces other child threads;
- Typically, it must finally finish executing because it performs various shutdown 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 invoke Method CurrentThread () to obtain a reference to it, CurrentThread () 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 invoked it. Once you get the main thread's reference, you can control the main thread as you 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, a reference to the current thread (naturally the main thread) is obtained by calling CurrentThread (), which is saved in the local variable T. The program then displays the thread's information. Then the program calls SetName () to change the thread's internal name. The thread information is displayed again. Then, a number of loops is decremented starting at 5, pausing one second at a time. Pauses are done by the sleep () method. The sleep () statement explicitly stipulates that the delay time is 1 milliseconds. Notice the Try/catch block outside the loop.
The sleep () method of the thread class may throw a interruptedexception exception. This situation occurs when other threads want to disturb the sleeping thread. This example only prints the message that it was interrupted. In the actual program, 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]
5
4
3
2
1
Note that the output of T is used as the parameter of the statement println (). The display order: the thread name, priority, and the name of the group. By default, the main thread's name is main. Its priority is 5, which is also the default value, and main is also the name of the owning thread group. A thread group is a data structure that controls the state of a thread as a whole set. This process is handled by the proprietary runtime environment and is not discussed here. After the thread name is changed, T is output 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 program. The Sleep () method directs the thread from being invoked to pending, according to the millisecond-level time. Its usual form is as follows:
static void sleep (long milliseconds) throws Interruptedexception
The pending time is explicitly defined as milliseconds. The method may throw a interruptedexception exception.
The Sleep () method also has a second form, shown below, which allows you to specify whether the time is in milliseconds or nanosecond cycles.
static void sleep (long milliseconds, int nanoseconds) throws Interruptedexception
The second form is available only if the time period is allowed in nanoseconds. As shown in the above procedure, you can use SetName () to set the thread name and use GetName () to get the thread name (the 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 thread name.