20165223 "Java Programming" Eighth Week Java Learning Summary

Textbook learning content summary

Chapter 12 - Java Multithreading Mechanisms

gist

  • Threads in Java
  • Thread class and thread creation
  • Common methods of threading
  • thread synchronization
  • coordinating synchronized threads
  • thread union
  • GUI thread
  • timer thread

Problems and Solving Processes in Teaching Materials Learning

1. Processes and threads

  • Program: static code, blueprint for application execution
  • Process: a dynamic execution process of a program
  • Thread: a smaller execution unit than a process, a process can generate multiple threads; the terminal and recovery of threads can save system overhead; multi-threaded processes are more practical

2. Main thread

  • That is, the main thread is the thread that is started after the JVM loads the code and finds the main method.
  • When the main method returns, the JVM ends the Java application (the virtual machine automatically exits)
  • The JVM waits until all threads in the Java program have ended before ending the Java application

3. Thread state and life cycle

The Java language uses objects of the Thread class and its subclasses to represent threads

  • four states
    • New : An object of the Thread class or its subclasses is declared and created, and it already has the corresponding memory space and other resources
    • run :
      • When the JVM switches the CPU usage right to the thread, the thread can start its own life cycle independently of the main thread that created it
      • After the thread is created, it only occupies memory resources, and the start() method (the method inherited from the parent class) must be called to notify the JVM that the new process is queued
      • The program must rewrite the run() method of the parent class in the subclass. Before the thread ends the run() method, do not let the thread call the start() method again, otherwise an IllegalThreadStateException will occur.
    • Interruption : Four 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, the sleep(int millsecond) method is executed to make the current thread go to sleep
      • The wait() method is executed, so that the current thread enters the waiting state, and other threads must call the notify() method to notify it, so that it re-enters the thread queue to queue for CPU resources
      • Performing an operation to enter the blocking state, such as performing a read/write operation causes blocking. Only when the cause of the blocking is eliminated, the thread re-enters the thread queue to queue for CPU resources
    • Death : After the thread ends, it no longer has the ability to continue running, and the memory allocated to the thread object is released. Two causes of death:
      • The run() method ends after normal execution
      • The run() method is forcibly terminated early

4. Thread scheduling and priority

  • If the priority level of the thread is not explicitly set, the priority is 5 by default, that isThread.NORM_PRIORITY
  • The thread priority setPriority(int grade)is adjusted by the method, an int parameter is required, and the parameter range is within 1~10, that is, in the Thread.MIN_PRIORITYsum Thread.MAX_PRIORITY, if it is no longer in this range, an exception will be IllegalArgumenExceptionthrown

5. Thread class and thread creation

  • Thread class
    • Construction method:Thread(Runnable target)
    • Parameters: The parameter of the constructor is an interface of type Runnable
    • Instance: To pass an instance of the Runnable interface class to the parameter

6. Common methods of threads

  • start() : The thread calls this method to start the thread and make it queue from the new state to the ready queue. Once it is its turn to enjoy 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 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, thus ending the sleep and re-queuing for CPU resources

7. Coordinate Synchronized Threads

  • Thread synchronization: that is, several threads need to use a synchronized modified method. That is, several threads in the program need to use a method, and this method is modified with synchronized
  • Synchronization method: wait(), notify(), notifyAll(). All are final methods of the Object class, inherited by all classes and not allowed to override
  • cannot be used in asynchronous methods wait(), notify(),notifyAll()

Problems and solutions in code debugging

thinking and perception

This chapter learned about another major feature of Java - threads. After learning, I learned that mastering multi-thread programming technology can make full use of CPU resources, and it can be applied more widely and more conveniently in practical problems.

code hosting

For details, see Code Cloud: Code Cloud Jobs

learning progress bar

Lines of code (added/accumulated) Blog volume (new/accumulated) Study time (added/accumulated) important growth
Target 3000 lines 30 articles 400 hours
eighth week 1324/200 2/11 20/20 in-depth

References

  1. Instructional video
  2. Intellj IDEA Simple Tutorial

Guess you like

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