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

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

Textbook learning content summary

Ch12

  • process and thread
    • A thread is a smaller unit than a process. A process can generate multiple threads during its execution.
  • Threads in Java
    • main thread

      Every Java application has a default main thread. When the JVM loads the code and finds the main method, it starts a thread, called the "main thread" (main thread), which is responsible for executing the main method.
    • Thread state and life cycle

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

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

      1. New: When an object of the Thread class or its subclasses is declared and created, the new thread object is in a new state.

      2. 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.

      3. Interruption: There are 4 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.

      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. Death: The thread in the dead state does not have the ability to continue running, freeing the memory allocated to the thread object

  • Thread class and thread creation
    • In the Java language, use the Thread class or subclass to create a thread object. When writing a subclass, you need to override the run() method.
    • 使用Thread类:Thread(Runnable target)
  • Common methods of threading
    • 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, 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, thereby ending the sleep.
  • thread synchronization
    • Several threads need to use a method at the same time and need to be modified with synchronized. When a thread uses the synchronized method, if other threads want to use it, they 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, they will execute the notifyAll() method to notify all threads in the waiting state to end the waiting when the method is used up.
  • 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.
    The wait(), notify(), notifyAll() methods cannot be used in unsynchronized methods.
  • thread union
    • When a thread A occupies CPU resources, it can let other threads call join() to join with this thread, such as:
      B.join(); It is
      said that A joins B during operation.
    • If thread A joins thread B while it occupies CPU resources, thread A will immediately interrupt execution until the thread B it joins with is executed, and thread A will re-queue for CPU resources in order to resume execution.
    • B.join() will have no effect if the B thread that A is preparing to join has ended.
  • GUI thread
    • When a Java program contains a graphical user interface (GUI), the Java virtual machine automatically starts more threads while running the application
    • AWT-EventQueue thread is responsible for handling GUI events
    • The AWT-Windows thread is responsible for drawing the form or component to the desktop
  • timer thread
    • 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()
  • Daemon thread
    • A thread can call the void setDaemon(boolean on) method to set itself as a daemon thread.
    • You must set yourself whether to daemonize the thread before running.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: After reading the textbook, I do not understand the purpose and meaning of JDBC

Problems and solutions in code debugging

  • Question 1: Example11_1 reports an
    error
  • Problem 1 solution:

Be sure to open cmd as an administratormysqld --initialize-insecure , and then execute the command. If the command has been executed as a non-administrator before, be sure to delete the original data folder. If it prompts "The file is in use", you need to CTRL+SHIFT+ESCopen the task manager and close the mysql process to delete the data folder normally.

code hosting

Summary of last week's exam mistakes

1. The interface RowSet inherits the interface __, and the method __ in RowSet can be called to fill its data area.
①Statement ②ResultSet ③update ④populate

A . ①③

B . ①④

C . ②③

D . ②④

Correct Answer: C

Analysis: The populate method can only be used to write data

2. Which of the following is a data manipulation language

A . insert

B . update

C . create

D . select

Correct Answer: AB

My answer: ABD

Analysis: I don't quite understand why there is no D in this question. According to Baidu Encyclopedia's definition of data manipulation language , select should also belong to the manipulation language?

learning progress bar

Lines of code (added/accumulated) Blog volume (new/cumulative) Study time (added/accumulated)
Target 5000 lines 30 articles 400 hours
the first week 24/24 1/4 15/15
the second week 423/423 2/6 14/29
The third week 702/1109 1/7 16/45
the fourth week 918/2027 2/9 16/61
fifth week 1208/3235 2/11 15/76
Week 6 1137/4372 2/13 14/90
Week 7 549/4921 1/14 10/100
eighth week 1025/5946 3/17 15/115

Guess you like

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