20165302 Eighth Week Summary

20165302 Eighth Week Summary

process and thread

A thread is a smaller execution unit than a process. A process can generate multiple threads during its execution, forming multiple execution threads. Without a process, there would be no thread, just as there would be no process without an operating system.

main thread

  • When the JVM loads the code and finds the main method, it starts a thread, called the main thread, which is responsible for executing the main method.

Thread process and life cycle

  • new
  • run
  • Interruption: There are four reasons for interruption: JVM switches CPU resources from the current thread to other threads, so that the thread gives up the right to use the CPU and is in an interrupted state. While the thread is using CPU resources, the sleep(intmillsecond) method is executed to make the current thread go to sleep.
    While the thread is using CPU resources, the wait() method is executed.
    While the thread is using CPU resources, it executes an operation and enters the blocking state.
  • die

Common methods of threading

  • start(): When the thread calls this method, it will start the thread and enter the ready queue from the new state. Once it is its turn to enjoy the CPU resources, it can start its own life cycle independently of the thread that created it.
  • run(): The run() method of the Thread class has the same function and function as the run() method in the Runnable interface. Both are used to define the operations performed after the thread object is scheduled, which are automatically called by the system and cannot be referenced by the user program. Methods.
  • sleep(int millsecond): A thread with high priority can call the sleep method in its run() method to make itself give up CPU resources and sleep for a period of time.
  • isAlive(): When the thread is in the "new" state, the thread calls the isAlive() method and returns false. Before the thread's run() method ends, that is, before it enters the dead state, the thread calls the isAlive() method to return true.
  • currentThread(): This method is a class method in the Thread class and can be called with a class name. This method returns the thread that is currently using CPU resources.
  • interrupt() : A thread that occupies CPU resources can let the sleeping thread call the interrupt() method to "wake up" itself, that is, cause the sleeping thread to have an InterruptedException exception, thereby ending the sleep and re-queuing for CPU resources.

synchronization of threads

  • Multiple threads call the synchronized method mechanism: when one thread uses the synchronized method, other threads must wait when they want to use the method

thread union

  • Once A joins B while occupying CPU resources, it can use B.join() to let other threads join this thread

coordinating synchronized threads

  • The wait() method can interrupt the execution of the method, make the thread wait, temporarily give up the right to use the CPU, and allow other threads to use this synchronization method.
  • The notifyAll() method notifies all threads that are waiting due to the use of this synchronization method to end waiting. The thread that has been interrupted will continue to execute this synchronization method from the point where it was interrupted just now, and follow the principle of "interrupt first, continue first".
  • The notify() method just notifies one of the waiting threads that one of them has finished waiting.

Daemon thread

  • A thread can set itself as a daemon thread by calling the void setDaemon(boolean on) method

code hosting

Guess you like

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