20165324 "Java Programming" Eighth Week Learning Summary

Student number 20165324 "Java programming" eighth week study summary

Textbook learning content summary

Chapter 12 Java Multithreading Mechanism

process and thread

进程是程序的一次动态执行过程;线程是比进程更小的执行单位

Thread state and life cycle

  • The Java language uses objects of the Thread class and its subclasses to represent threads, and the complete life cycle goes through the following states:
    1. new
    2. Running: You must call the start() method to notify the JVM to run
    3. Disruption: Four Reasons Disruption
    4. Death: There are two reasons, one is the end of normal operation; the other is that the thread is forced to end early, that is, the run() method is forced to end.

Thread scheduling and priority

  • The thread scheduler in the Java virtual machine is responsible for managing threads, and the priority is divided into 10 levels, which are represented by class constants in the Thread class. The priority can be setPriority(int grade)adjusted by the method, getPrioritywhich returns the priority of the thread.
  • The task of the JVM's thread scheduler is to enable high-priority threads to run all the time. Once the time slice is free, threads with the same priority will use the time slice in turn.

Thread class and thread creation

  • Use a subclass of Thread: When writing a subclass of the Thread class, you need to override the run()methods of the parent class to standardize the specific operations of the thread.
  • Use the Thread class: the constructor is Thread(Runnable target), the parameter in the constructor is an interface of type Runnable.

Using the Runnable interface is more flexible than using a subclass of the Thread class

  • If a class inherits Thread, it is not suitable for resource sharing. But if the Runable interface is implemented, it is easy to achieve resource sharing.

The relationship between the target object and the thread

  • The target object is completely decoupled from the thread
  • Target object composition thread (weak coupling)

About the number of times the run method is started

Common methods of threading

  • start(), call the method to start the thread and make it enter the ready queue to queue
  • run(), used to define the operation performed after the thread object is scheduled
  • sleep(int millsecond), Call the method to make it give up CPU resources and sleep. The sleep() method must be called in a try-catch statement.
  • isAlive(), when the thread is in the new state, the thread calling isAlive()method returns false.
  • currenThread()The method is a class method of Thread, which can be called with the class name and returns the thread that is currently using CPU resources.
  • interrupt(), call this method to end sleep and re-queue for CPU resources.

thread synchronization

  • Concept: Several threads in the program need to use a method, and this method is decorated with synchronized. Multiple threads calling this method must obey the synchronization mechanism.

coordinating synchronized threads

  • The wait() method is used in the synchronization method to interrupt the execution of the thread.
  • The notify() method is used in the synchronization method to notify all threads that are waiting due to the use of this synchronization method to end waiting.
  • wait(),notify(),notifyAll()都是Object类中的final方法

thread union

  • During the period when a thread A occupies CPU resources, it can let other threads call join() to join this thread, such as: B.join();.

GUI thread

  • When a Java program contains a graphical user interface (GUI), when the Java virtual machine runs the application, it will automatically start more threads, among which there are two important threads: AWT-EventQuecueand AWT-Windows, the former is responsible for GUI events, and the latter thread is responsible for converting The form or component is drawn to the desktop.

timer thread

Daemon thread

  • A thread can call void setDaemon(boolean on)methods, set up as a daemon thread.

Problems and Solving Processes in Teaching Materials Learning

Problems and solutions in code debugging

code hosting

Summary of last week's exam mistakes

  • Error 1 and the reason, understand the situation
  • Error 2 and the reason, understand the situation
  • ...

References

Guess you like

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