20165205 2017-2018-2 "Java Programming" Eighth Week Learning Summary

20165205 2017-2018-2 "Java Programming" Eighth Week Learning Summary

Textbook learning content summary

  • process and thread
    • A thread is not a process, but behaves much like a process, and a thread is a smaller unit of execution than a process.
    • Unlike processes, the interruption and recovery of threads can save system overhead.
  • threads in java
  • A major feature of the Java language is its built-in support for multithreading.
  • The state and life cycle of the thread:
    • New: When the java language uses the object of the Thread class and its subclasses to be declared and created, the new thread object is in a new state.
    • Running and blocking: When the thread in the ready state obtains the CPU execution slice, it enters the running state, but during the execution process, the thread may enter the blocking state for the following reasons
      • The CPU execution slice has been used up, and the JVM switches to other threads for execution
      • The thread calls sleep()
      • The thread calls the blocking IO method, and the thread will block until the method returns
      • Thread attempts to acquire synchronization monitors held by other threads
      • Thread is waiting for a notification
      • The program calls the thread's suspend() to suspend the thread. (Easy to deadlock, not recommended)
    • After the thread enters the blocked state from running, it can only continue to block or enter the ready state again. The following conditions will cause the thread to re-enter the ready state from the blocked state.
      • The sleep() called by the thread has passed the specified time
      • The blocking IO method called by the thread returns
      • Thread successfully acquired synchronization monitor
      • Thread receives notifications from other threads
      • The thread that is suspended (suspend) is called the resume method by the program again
    • Death: After the thread ends, it will be in a dead state. The thread will end in the following three ways:
      • The normal execution of run() or call() is completed, and the thread ends normally
      • The thread throws an uncaught Exception or Error
      • Directly call the stop() method of the thread to end the thread, which is easy to deadlock
      • Note that once the child thread is started, its status is the same as that of the main thread, so once the main thread ends, the child thread will not be affected, and will not return true when the isAlive() method of the end thread object is ready, running, and blocked. , when new, return false when it dies. Calling start() on a thread that has died is invalid, and an exception will be thrown. A dead thread cannot be executed as a thread again. For newly created threads, calling the start() method twice will also throw an exception
  • Thread class and process creation
    • All JAVA threads must be instances of Thread or its subclasses. Methods as below:
      • Define the Thead subclass and implement the run() method, run() is the thread execution body
      • Creating this subclass instance object creates a thread object
      • Call the start() method of the thread object to start the thread
  • Common methods for threads:

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. 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
      When dealing with multithreading problems, we must pay attention to such a problem: when two or more threads access the same variable at the same time, and one thread needs to modify this variable. We should deal with such issues.

  The so-called thread synchronization is that several threads need to use a synchronized method. That is, several threads in the program need to use a method, and this method is decorated with synchronized.

  Multiple threads calling the synchronized method must obey the synchronization mechanism: when one thread uses this method, other threads must wait until the thread finishes using the method. When using multithreading to solve many practical problems, some methods of modifying data may be modified with the keyword synchronized.

  • thread union
    • The join method of threads means that one thread waits for the other thread to finish before executing. After the join method is called, the thread object is blocked. Some people also call this method a joint thread, which means that the current thread and the thread where the current thread is located are combined into one thread. Which thread calls this method, then this thread will execute first before executing other threads.

      The textbook learns the problems and solutions in code debugging:

  • Question 1: What is the difference between a thread and a process?
  • Problem 1 Solution: The process is static, a mirror
    thread is dynamic, a program will first open up threads when it is running, and then execute the methods inside to open up more new threads, while the process is used by the operating system to display programs to users A mechanism for running,
    so in actual development, whether it is java or other languages, there are only threads

  • Question 2: What is the difference between the start() method and the run() method?
  • Solution to problem 2: Only when the start() method is called will the multi-threading feature be exhibited, and the codes in the run() methods of different threads are executed alternately. If you just call the run() method, the code is still executed synchronously. You must wait for the code in the run() method of one thread to be executed completely before another thread can execute the code in its run() method.

code hosting

Summary of wrong questions

  • The exception types of JDBC programming are divided into AC

    • A.SQLException
    • B.SQLError
    • C.SQLWarning
    • D.SQLFatal
    • E.SQLTruncation
  • The type returned by executeUpdate is int, which means the affected record A
    • A.true
    • B.false
  • The following are commonly used databases are ACDE
    • A.Access
    • B.XAMMP
    • C.MySQL
    • D.Oracle
    • E.SQL

Summarize

This week, I learned about multi-threading in java. When I started learning, I was still thinking about why I should use multi-threading. Later, after learning, after I finished this chapter, I learned that this use can take advantage of multi-core cpu and prevent blocking. It is easy to model, and it will be much more convenient to be able to use multi-threading flexibly.

Guess you like

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