When the Java program starts, a thread runs immediately, which is often 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 ways:
- It is the thread that produces the other child threads;
- Usually it must finish execution as 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 Method CurrentThread () to obtain a reference to it, and CurrentThread () is the public static member of the thread class. Its usual form is as follows:
Static Thread CurrentThread ()
The 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 control other threads.
Let's start by reviewing the following example:
1 //controlling the main Thread.2 classCurrentthreaddemo {3 Public Static voidMain (String args[]) {4Thread T =Thread.CurrentThread ();5System.out.println ("Current thread:" +t);6 //Change the name of the thread7T.setname ("My Thread");8System.out.println ("After name change:" +t);9 Try {Ten for(intn = 5; n > 0; n--) { One System.out.println (n); AThread.Sleep (1000); - } -}Catch(interruptedexception e) { theSystem.out.println ("Main thread Interrupted"); - } - } -}
In this program, a reference to the current thread (which is naturally the main thread) is obtained by calling CurrentThread (), which is saved in the local variable T. The program then displays information about the thread. Then the program calls SetName () to change the thread's internal name. The thread information is displayed again. Then, a number of loops decreases from 5, pausing for one second each time. The pause is done by the sleep () method. The Sleep () statement explicitly specifies that the delay time is 1 milliseconds. Note 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 just prints the message that it was interrupted. In a real-world program, you have to deal with this kind of problem flexibly. 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 T as the output of the arguments in the statement println () when applied. The display order: the thread name, the priority, and the name of the group. By default, the name of the main thread is main. It has a priority of 5, which is also the default, 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 collection. This process is handled by a proprietary runtime environment and is not discussed here. Once 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 instructs the thread to suspend from being called, 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. This method may throw an interruptedexception exception.
The Sleep () method also has a second form, which is 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 available only when the time period is allowed in nanoseconds. As shown in the above program, you can use SetName () to set the thread name and GetName () to get the thread name (which 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.
Series Articles:
Java know how much (top)
Java know how much (interface) interface
Java knows how much (40) the difference between an interface and an abstract class
Java know how much (41) generic explanation
Java know how much (42) the range of generic wildcard characters and type parameters
Java know how much (43) Exception Handling Basics
Java know how much (44) exception type
Java know how much (45) uncaught exceptions
How much Java knows (the) use of try and catch
Java know how much (47) use of multiple catch statements
Java knows how much (in) the nesting of Try statements
Java know how much (a) throw: Exception throws
Java know how many () Java throws clauses
Java knows how many (or) finally
Java know how much (52) built-in exceptions
Java know how much (53) Use Java to create your own exception subclasses
Java know how much (54) assertion detailed
Java know how many (55) threads
Java know how much (56) threading Model
Java know how much (57) main thread