20165334 "java programming" week 8 learning summary

Student number 20165334 "Java Programming" Week 8 Learning Summary

Textbook learning content summary

Chapter 12 Java Multithreading Mechanism Knowledge Summary

Thread state and life cycle

A created thread usually goes through the following four states during its entire life cycle:

  • New: When an object of the Thread class or its subclasses is declared and created, the new thread object is in the new state.
  • Running: The thread must call the start() method (a method inherited from the parent class) to notify the JVM, so that the JVM will know that there is a new thread queued for switching. Once it is its turn to enjoy CPU resources, this thread can start its own life cycle independently of the main thread that created it.
  • Interruption: There are 4 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(int -millsecond) 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.

  4. Dead: A thread in a dead state does not have the ability to continue running. The thread frees the entity.

Two ways of thread creation

Use a subclass of Thread

When writing a subclass of the Thread class, you need to override the run() method of the parent class. The purpose is to specify the specific operation of the thread, otherwise the thread will do nothing, because there is no operation statement in the run() method of the parent class. .

2 Using the Thread class

The commonly used constructor to create a thread with Thread is:

    Thread(Runnable target)   

The parameter in this constructor is an interface of type Runnable.
When creating a thread object, an instance of a class that implements the Runnable interface must be passed to the parameter of the constructor. The instance object is called the target object of the created thread. After the thread calls the start() method, once it is its turn to enjoy the CPU resources, The target object will automatically call the run() method in the interface (interface callback).

The relationship between the target object and the thread

1. The target object and the thread are completely decoupled

  The target object does not have a composite thread object. The target object often needs to get the thread name (since there is no way to get a reference to the thread object) in order to determine which thread is consuming CPU resources, that is, the thread that is being executed by the JVM.
String name=Thread.currentThread().getNmae()

2. Target object composition thread (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.
``Thread.currentThread()

Common methods of threading

  • start() : When a 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, thus ending the sleep and re-queuing for CPU resources

    Thread synchronization is a method in which several threads need to use a synchronized modification.

    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.

code hosting

(screenshot of the running result of the statistics.sh script)

Summary of last week's exam mistakes

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated) important growth
Target 5000 lines 30 articles 400 hours
the first week 200/200 2/2 20/20
the second week 300/500 2/4 18/38
The third week 500/1000 3/7 22/60
the fourth week 300/1300 2/9 30/90
fifth week 1000/1300 2/9 30/90
Week 6 900/1300 2/9 30/90
Week 7 600/1300 2/9 30/90
eighth week 800/1300 2/9 30/90

References

Guess you like

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