SoFunction
Updated on 2025-03-08

Analysis of new and ready state instances of Java threads

This article describes the creation and ready state of Java threads. Share it for your reference, as follows:

A finishing touch

When a thread is created and started, it neither enters the execution state as soon as it is started nor is it always in the execution state. During the life cycle of the thread, it must go through five states: new creation, ready, running, blocking and death. When a thread starts, it cannot always occupy the CPU and run it alone, so the CPU needs to switch between multiple threads, so the thread state will also switch between running and ready many times.

When the program uses the new keyword to create a thread, the thread is in a new state.

When the thread object calls the start() method, the thread is in the ready state. The thread in this state does not start running, but just means that the thread can run. As for when the thread starts running, it depends on the scheduler in the JVM.

Automatic threads use the start() method, not the run() method, and never call the run() method of the thread object. Otherwise, the system treats the thread object as a normal object, and the run() method is also an ordinary method, not a thread execution body.

Two code

public class InvokeRun extends Thread
{
   private int i ;
   // Rewrite the run method, the method body of the run method is the thread execution body   public void run()
   {
      for ( ; i < 100 ; i++ )
      {
        // When the run method is called directly, Thread returns the object name.        // Instead of the name of the current thread.        // Use().getName() to always get the current thread name        (().getName()
           + " " + i);  // ①
      }
   }
   public static void main(String[] args)
   {
      for (int i = 0; i < 100; i++)
      {
        // Call Thread's currentThread method to get the current thread        (().getName()
           + " " + i);
        if (i == 20)
        {
           // Directly call the run method of the thread object,           // The system will treat thread objects as ordinary objects and run methods as ordinary methods.           // So the following two lines of code will not start two threads, but execute two run methods in turn           new InvokeRun().run();
           new InvokeRun().run();
        }
      }
   }
}

Three Run

......
main 93
main 94
main 95
main 96
main 97
main 98
main 99
main 0
main 1
main 2
main 3
main 4
main 5
......

Four Description

After the above program creates the object, the run() method of the thread object is called directly. The result of the program running is that there is only one thread in the entire program: the main thread.

If the run() method of the thread object is called directly, the run() method cannot directly obtain the name of the current execution thread through the getName() method. Instead, you need to use the () method to obtain the current thread first, and then call the getName() method of the thread object to obtain the name of the thread.

The correct way to start a thread is to call the start() method of the Thread object, rather than directly calling the run() method, otherwise it will become a single-threaded program.

After calling the run() method, the thread is no longer in the new state. Do not call the start() method of the thread object again.

The start() method can only be called on threads in the newly created state, otherwise an exception will be raised.

If you want the child thread to start execution immediately after calling the start() method of the child thread, the program can use (1) to make the currently running thread (main thread) sleep for 1 millisecond, because the CPU will not be idle within 1 millisecond, and it will execute another thread in the ready state, so that the child thread can start execution immediately.

For more Java-related content, please view the topic of this site:Summary of Java process and thread operation skills》、《Java Data Structure and Algorithm Tutorial》、《Summary of Java operating DOM node skills》、《Summary of Java files and directory operation skills"and"Summary of Java cache operation skills

I hope this article will be helpful to everyone's Java programming.