20165308 Eighth week study summary

20165308 Eighth week study summary

Summary of textbook knowledge points

  • Process: code loaded, executed, executed (shared operating system resources)
  • Threads: A process consists of multiple threads. (share some memory units in the process, such as code and data)
  • Relationship: A thread is a small thread running in a process.
  • Multithreading: A process in which several executors jointly execute a program.
    • Multithreading uses a round-robin execution mechanism
    • 1 CPU can only execute one thread at a time.
  • Main thread: The thread started after the main method is found when the JVM loads the code
  • Status of the thread:
    • New: An object of the Thread class or its subclasses is declared and created
    • Run: call the start() method to notify the JVM; if the thread is created by a subclass of Thread, the program must override the run() method in the subclass
    • Interrupt:
    • JVM talks about switching CPU to other threads
    • Execute the sleep(int millsecond) method, wait for the specified time of millsecond, and then queue up again
    • To execute wait(), the notifyll method must be called by the thread to be requeued
    • into blocking state
    • die: free memory allocated to thread
    • Normal death, after running run()
    • forced to terminate early
  • thread creation
    • Created using a subclass of the Thread class: you need to override the run() method.
    • Use the Thread class
    • Constructor: Thread(Runable target)
    • You need to create an instance of the Runnable interface class and override the run method in this interface.
  • The relationship between target objects and threads: complete decoupling and target object composition threads
  • Common methods for threads:
    • 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. They are all methods that are automatically invoked by the system and must not be referenced by user programs.
    • 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, which can be called with the 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.
  • Thread synchronization: Several threads need to use a synchronized (synchronized) modified method
  • thread synchronization mechanism
  • When a thread uses a synchronizedmethod, if other threads want to use it, it must use the wait()method to wait until the thread finishes using the method
  • If other threads do not need to wait when using the synchronization method, when the method is used up, the execution notifyAll()method will notify all the threads in the waiting state to end waiting
    . The wait(), notify(), notifyAll() methods must be used in the synchronization method
  • Thread association
    join(): other threads call this method to associate with threads that are using CPU resources.
  • GUI thread
  • Timer thread: use the Time class
  • Daemon thread: how to use void setDaemon(boolean on)it

    Code cloud link

https://gitee.com/BESTI-IS-JAVA-2018/5308/tree/master/src/ch12

Script screenshot

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326574020&siteId=291194637