The Main Thread
When a Java program starts up, one thrc~d begins running immediately. This is usually called the main thread of your program, because it is the one that is executed when your program begins. The main thread is important for two’ reasons: .
• It is the thread from which other “child” threads will be spawned.
• It must be the.last thread to finish execution. When the main thread stops, your
Although the main thread is created automatically when your program is started, it can be controlled through a Thread object. To do so, you must obtain: a reference to it by calling the method currentThread( ), which is a public static member, of Thread. Its general form is shown here:
.static Thread currentThread( )
This method returns a reference to the thread in which it is called. Once you have a reference to the main thread, you can .control it just like,any other thread, Let’s begin by viewing the following example:
In this program, a reference to the current thread (the main thread, in this case) is obtained by calling current Thread ), and this reference is stored in the 1000al variable t. , Next, the program displays information about the thread ..The program then calls setNjUne( ) to change the internal name of the thread. Information about the thread is then redisplayed. Next, a loop counts down from five, pausing’ one second between each line. The pause is accomplished by the sleep method. The argument to sleep specifies the delay period in milliseconds. Notice the try catch block around this loop The sleep method in Thread might throw an Interrupted Fl Exception. This would happen jf some other thread wanted to interrupt this sleeping one. This example just prints a message if it gets interrupted. In a real program, you would need to handle this differently. Here is the output generated by this program:
Current thread: Thread[main 5 main , After ,name change: Thread[My Thread 5 main
Notice the Output produced when t is used as imargument to println( ) This displays ir order: the name of the thread; its priority.and the name of·its grQup. By default, the .name of the main thread is main. Its priority is 5, which is the default value, and main is also the name oj the group of threads to which this thread belongs, A thread group is a data structure that controls-the state of a collection of threads as a whole This process is managed by the particular run-time environment and is not discussed in detail here. ,-After the name of the thread is changed, t is again output. This time, the’ new name of the thread is-displayed, “
Let’s look more closely at the methods defined by Thread that are used in the program. The sleep ()method causes the thread from which it is called to suspend execution for the specified period-of milliseconds. Its general form is shown here static void sleep long milliseconds) throws Interruptedexception The number of milliseconds to suspend is specified in milliseconds. This method may throw anInterruptedbxceptlcn. The sleep() method has a second form, shown next, which allows you to specify the period in terms of milliseconds and nanoseconds static void sleep long milliseconds, int ttanosecolld’s)’throws Interruptedexception This second form is useful only in environments that allow tinting periods as short as nanoseconds
As the preceding program shows, you can set the name of a thread by using setName(). You can obtain the name of a thread by calling getName( )'(but note that .this procedure is not shown in the program). These methods arc members of the Thread class and are declared like this
final void se~Name(511:ingthrcl1dNal11c)
final String getName( )
Here, thread blame specifies the name of (he thread.