Main thread:
By the main thread is established automatically whenever your program is begin, it can be controlled by a Thread object. To do this, you must allow a reference to it through calling the method current Thread ( ), that is a public static member of Thread. Its common form is display here:
static Thread currentThread ( )
This method returns a reference to the thread in that it is called. At one you have a reference to the main thread; you could control it only such any other thread.
Let's starts through reviewing the given example:
/ / 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 interrrupted");
}
}
}
In the above program, a reference, to the current thread (the main thread, in this case) is obtained through calling currentThread ( ) and this reference is stored in the local variable t. Further, the program displays data about the thread. A program then calls setName( ) to modify the internal name of the thread. Information about the thread is then redisplayed. Further, a loop counts down from five by pausing one second among each line. The pause is accomplished through the sleep ( ) method. An argument to sleep ( ) specifies the delay period in milliseconds. Remember the try/catch block around this loop. The sleep ( ) method in Thread may throw an Interrupted Exception. This would occur if a few other thread needed to interrupt this sleeping one. This instance just prints a message if it gets interrupted. Within a real program, you would require to handle this differently. Here is the output produced through this program.
Current thread: Thread [main, 5 main]
After name change: Thread [My Thread, 5, main]
5
4
3
2
1
Remember the output generate when it is used as an argument to println( ). This displays, sequence the name of the thread, its priority and the name of its group. By default, the name of the main thread is main. The priority of that main thread is 5, that is the default value, and main is also the name of the group of threads to that this thread belongs. A thread group is a data structure which controls the state of a collection of threads as an overall. This process is managed through the particular run-time environment and is not discussed in detail here. After the name of the thread is modified, t is again output. This time, the new name of the thread is shown.
Let's see more closely at the methods defined through Thread which are used in the program. A sleep ( ) method causes the thread from that it is known to suspend execution for the specified period of milliseconds. Its common form is display here:
static void sleep(long milliseconds) throws InterruptedException
A number of millisecond to suspend is specified in milliseconds. These functions might throw an Interrupted Exception.
The sleep ( ) function has a second form, shown next, that permits you to specify the period in terms of millisecond and nanoseconds.
static void sleep ( long millliseconds, int nanoseconds) throws Interrupted Exception.
Another form is useful only in environment which permits timing periods as short as nanoseconds.
As the preceding program displays, you can set the name of a thread via using setName ( ). You can get the name of a thread through calling getName( ) (but note that this procedure is not display in the program..) These methods are members of the Thread class and are declared like this:
final void setName (String threadName)
final String getName ( )
In the above example, threadName specifies the name of the thread.