20165330 2017-2018-2 "Java Programming" Week 8 Learning Summary

Textbook Knowledge Summary

Chapter 12 Java Multithreading Mechanism

  1. Threads in Java
  • Process: It is a dynamic execution process of a program, which corresponds to a complete process from code loading, execution to execution completion
  • Thread: a process can generate multiple threads during its execution, forming multiple execution threads, each thread has its own process of generation, existence and death
  • Multithreading: refers to the existence of an executive body in an application at the same time
  • Thread state and life cycle
    • New: Use objects of the Thread class and its subclasses to represent threads
    • run: run()
    • interrupt
    • die: free memory allocated to thread object
  • The priority of each Java thread is constant between 1 and 10, and if the priority is not explicitly set, the priority of each thread is the constant 5.
  1. Thread class and thread creation
  • In the Java language, use the Thread class or subclass to create a thread object
  • 使用Thread类:Thread(Runnable target)
  • The relationship between the target object and the thread
    • The target object is fully coupled to the thread
    • The target object combines threads (weak coupling), that is, threads as their own members
  1. Common methods of threading
  • start(): start the thread
    • Only threads in the newly created state can call the start() method
    • The thread does not have to call the start() method afterward, otherwise it will cause an IllegalThreadStateException
  • run()
    • Used to define the operation performed after the thread object is scheduled
    • When the run method finishes executing, the thread becomes dead
  • sleep (int millsecond)
    • Used to sleep high-priority threads and make low-priority threads do work
    • The sleep statement must be called within the try-catch statement
    • millsecond is the sleep time in milliseconds
  • isAlive()
    • When the thread is in the new state, calling isAlive() returns false
    • Before run() finishes, calling isAlive() returns true
    • After run() finishes, calling isAlive() returns false
  • currentThread(): Returns the thread that is using CPU resources
  • interrupt(): End the sleep of some threads and re-queue for CPU resources
  1. thread synchronization
  • Several threads need to use a synchronized (synchronized) modified method
  • Synchronization mechanism: when a thread A uses the synchronized method, other threads must wait until thread A finishes using the synchronized method when other threads want to use the synchronized method
  1. coordinating synchronized threads
  • Use the wait() method to interrupt the execution of the thread, make the thread wait, and temporarily give up the right to use the CPU
  • Use the notifyAll() method to notify all threads that are waiting due to using a synchronized method to end waiting
  1. thread union
  • When a thread A occupies CPU resources, it can let other threads call join() to join this thread
B.join()
  • After the union, thread A interrupts execution immediately, waits until thread B finishes executing, and then re-queues to wait for CPU resources
  • After B thread ends, B.join() will have no effect
  1. GUI thread
  • When a Java program contains a graphical user interface (GUI), the Java virtual machine automatically starts more threads while running the application
  • two important threads
    • AWT-EventQueue: responsible for handling GUI events
    • AWT-Windows: responsible for drawing the form or component to the desktop
  1. timer thread
  • Constructor: Timer(int a, object b), the unit of a is milliseconds, and b is the monitor of the timer
  • Ring the bell only once: call the setReapeats(boolean b) method, and the value of b is false
  • Start the timer: start()
  • Stop the timer: stop()
  • Restart the timer: restart()

Problems encountered and solutions

  1. When testing the database in class, the input Chinese and the final output are garbled characters.

    Solution: I did not prepare well before the class, and did not read the steps in the book carefully. When creating a new database, choose the corresponding Chinese character code gb2312 (GB2312 Simplified Chinese)

  2. When running the seventh code in this chapter, I found no output
    image

    Solution: After checking the code, I found myself careless without entering the variable name in the calling method

  3. When running the 5th code, the program keeps jumping time and can't jump out. How to make a thread stop running?

    Solution: Refer to how to terminate the running thread correctly and gracefully in Java. Only the start() method is called in the source program, and the program constantly allocates new entities. See the sixth code, you can call interrupt() to end some programs. Sleep interrupts the running thread.

code hosting

image

Summary of last week's mistakes

  1. image

    Parsing: Answer ACDE

  2. image

    Analysis: Answer C

  3. image

    Parsing: The return value of executeUpdate is an integer indicating the number of rows affected (ie the update count)

Guess you like

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