Home>

When the java program starts,One thread runs immediately,This thread is usually called the main thread of the program because it is executed when the program starts.The importance of the main thread is reflected in two aspects:

It is the thread that spawns other child threads; Usually it has to finish execution last,Because it performs various closing actions.

Although the main thread is created automatically when the program starts,But it can be controlled by a thread object.To do this, you must call the method currentthread () to get a reference to it,currentthread () is a public static member of the thread class.Its general 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 as you would any other thread.

Let's start by reviewing the following example questions:

//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 procedure,A reference to the current thread (naturally the main thread) is obtained by calling currentthread (), which is stored in a local variable t. The program then displays information about the threads.The program then calls setname () to change the internal name of the thread.The thread information is displayed again.Then, the number of cycles decreases from 5,Pause one second each time.Pause is done by the sleep () method.The sleep () statement specifies that the delay time is 1 millisecond. Note the try/catch block outside the loop.

The sleep () method of the thread class may raise an interrupted exception. This happens when other threads want to disturb the sleeping thread.This example just prints a message if it was interrupted.In the actual program,You have to be flexible in dealing with such issues.Here 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 the output of t when used as a parameter in the println () statement.The display order:thread name,Priority and the name of the group.by default,The name of the main thread is main. Its priority is 5, which is also the default.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.This process is handled by a proprietary runtime environment,I won't go into details 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 makes the thread go from being called to suspending according to the millisecond time indication.Its general form is as follows:

 static void sleep (long milliseconds) throws interruptedexception

Pending time is explicitly defined as milliseconds.This method may raise an interruptedexception exception.

The sleep () method has a second form,The display is as follows,This method 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 only available when nanosecond time periods are allowed.As shown in the above procedure,You can set the thread name with setname (),Use getname () to get the thread name (this process is not reflected in the program). These methods are members of the thread class,The statement is as follows:

 final void setname (string threadname)
  final string getname ()

Here, threadname refers specifically to the thread name.

  • Previous php word guessing game
  • Next Detailed usage of isAlive () and join () in Java thread programming