Analysis of various state transitions of threads





A thread will be in various states during its life cycle: new, waiting, ready, running, blocked, dead.
1. The new
   thread object created with the new statement is in the new state. At this time, it is only allocated memory like other java objects.
2. Wait
  When the thread is in the new state and before calling the start method, the thread is in a waiting state.
3. Ready
  When a thread object is created, other threads call its start() method, and the thread enters the ready state. Threads in this state are in the runnable pool of the Java virtual machine, waiting for the right to use the CPU.
4.
  The thread running in this state occupies the CPU and executes the program code. In a concurrent runtime environment, if the computer has only one CPU, only one thread will be in this state at any time.
  Only threads in the ready state have a chance to go to the running state.
5. Blocking state
  Blocking state means that the thread gives up the CPU for some reason and temporarily stops running. When a thread is in a blocked state, the Java virtual machine does not allocate CPU to the thread until the thread re-enters the ready state, it will have a chance to obtain the running state.
6. Dead state
  When the thread finishes executing the code in the run() method, or encounters an uncaught exception, it exits the run() method and enters the dead state, and the thread ends its life cycle.

  Since java thread scheduling is not time-sharing, if the program wants to intervene in the thread scheduling process of the java virtual machine, so as to explicitly allow one thread to give another thread a chance to run, the following methods can be used:
    1. Adjust the priority of each thread
    2. Let the thread in the running state call the Thread.sleep(long time) method to give up the CPU and enter the blocking state. The
       sleep method may throw InterruptedException
       . After the thread sleeps, the thread can only be made ready after the specified time. (That is, waiting for cpu scheduling)
    3. Let the thread in the running state call the Thread.yield() method, which will only yield with the priority or higher priority (enter the ready state)
    4. Let the thread in the running state call another The join() method of one thread The
       currently running thread can call the join() method of another thread, and the currently running thread will go to the blocking state, until the other thread finishes running, it will go to the ready state and have a chance to resume run.

  A thread can go from a blocked state to a runnable state in one of several ways.
     1. The thread is put to sleep for the specified number of milliseconds.
     2. The thread is waiting for the completion of the I/O operation, and the operation has been completed.
     3. The thread is waiting for a lock held by another thread, and another thread has released the ownership of the lock; (It is also possible to wait for a timeout. When the timeout occurs, the thread is unblocked.)
     4. The thread is waiting for a trigger condition , and another thread signals that the condition has changed. (If a timeout is set for the thread's waiting, the thread will be unblocked when the timeout occurs.)
     5. The thread has been suspended and its resume method has been called. However, since the suspend method is obsolete, the resume method is also deprecated, and you should not call it in your own code. (It should now be replaced by sleep.)

Mass video sharing vue angular dubbo



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326107190&siteId=291194637