20165323 Eighth week study summary

Textbook learning content summary

1. A program is a piece of static code, and a process is a dynamic execution process of a program.
2. A thread is a smaller execution unit than a process. A process can generate multiple threads during its execution, forming multiple execution clues. A clue that each thread also has its own process of birth, existence and death.
3. Multi-threading refers to the situation that several execution bodies exist in an application at the same time, and work together according to several different execution threads.
4. Each Java application has a single main thread.
5. When the JVM (Java Virtual Machine virtual machine) loads the code and finds the main method, it will start a thread, which is called the "main thread" (main thread), which is responsible for executing the main method.
6. The Java language uses objects of the Thread class and its subclasses to represent the thread
7. The state and life cycle of the thread: (1) new; (2) running; (3) interrupt; (4) death;

  • New : When an object of the Thread class or its subclasses is declared and created, the new thread object is in the new state. At this point it already has the corresponding memory space and other resources.
  • Running : 1. If the thread is created by a subclass of Thread, the run() method in this class is executed immediately, and the program must override the parent class's run() method in the subclass.
    2. Before the thread ends the run() method, do not let the thread call the start() method again, otherwise an IllegalThreadStateException will occur
  • Interruption : There are 4 reasons for interruption:
    The JVM switches the 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, a sleep(int millsecond)method is executed to put the current thread to sleep.
    A method was executed while the thread was using CPU resources wait().
    While the thread is using CPU resources, it executes an operation and enters the blocking state.
  • Death : The so-called dead state is that the thread releases the entity, that is, the memory allocated to the thread object is released. The thread in the dead state does not have the ability to continue running.
    8. The priority of the thread can be setPriority(int grade)adjusted by methods.
    9. The time when the thread uses the CPU resources After that time, even if the thread has not completed all its own operations, the JVM will interrupt the execution of the current thread and switch the usage rights of the CPU to the next queued thread. continue to execute.
    10. The advantage of using a Thread subclass to create a thread is that new member variables can be added to the subclass to make the thread have certain attributes
    . 11. Subclasses of the Thread class cannot extend other classes
    . The construction method is: Thread(Runnable target)
    13. The same memory unit can be shared between threads, and these shared units can be used to realize data exchange, real-time communication and necessary synchronization operations
    14. The relationship between the target object and the thread:
    (1) The target object and the thread are completely Decoupling: The target object does not have a combined thread object. The target object often needs to obtain the thread name (because the thread object reference cannot be obtained) in order to determine which thread is consuming CPU resources, that is, the thread that is being executed by the JVM.
    (2) The target object combines threads (weak coupling): The target object can combine threads. When the target object class combines thread objects, the target object can obtain a reference to the thread object through.
    15. Common methods of threads
    (1) start():When a thread calls this method, it will start the thread and enter the ready queue from the newly created state. Once it is its turn to enjoy CPU resources, it can start its own life cycle independently of the thread that created it. .
    (2)run():The function and function of the run() method of the Thread class is the same as that of the run() method in the Runnable interface. They are both used to define the operations performed after the thread object is scheduled. They are all methods that are automatically invoked by the system and must not be referenced by user programs.
    (3) 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, so that the thread with low priority has a chance to execute.
    (4) 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. When the thread enters the dead state, the thread calls the isAlive() method to return false.
    (5) currentThread():This method is a class method in the Thread class, which can be called with a class name. This method returns the thread that is currently using CPU resources.
    (6) interrupt() :A thread occupying 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.
    16. The so-called thread synchronization is a method in which several threads need to use a synchronized (synchronized) modification.
    17. Coordinate and synchronize threads:
    (1) wait()The 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.
    (2) notifyAll()The 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".
    (3) notify()The method just notifies one of the waiting threads to end waiting.
    (4) cannot be used in asynchronous methods wait(), notify()and notifyAll().
    18. When a thread A occupies CPU resources, it can let other threads call join()and associate with this thread.
    19. Non-daemon threads are also called user threads. A thread calling void setDaemon(boolean on)method can set itself as a Daemon thread.

code hosting

Guess you like

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