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

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

Textbook learning content summary

Threads in Java

  • The operating system can manage multiple processes at the same time, allowing multiple processes to share the resources managed by the system.
  • Threads can share certain memory units (including code and data) within the process. A process can have multiple threads within it take turns using the CPU.
  • When the JVM loads the code, after the main method is found, the "main thread" is started, which is responsible for executing the main method.
  • If other threads are created in the main method, the JVM will switch between the main thread and other counties in turn to ensure that each county has the opportunity to use CPU resources.
  • The JVM waits until all threads in the Java application have ended before ending the Java program.
  • Objects of the Thread class and its subclasses are used to represent threads, and the following four states are wrapped:
    1. New: Create a Thread subclass
    2. Run: The run() method must be rebuilt in the subclass.
    3. Interrupt: The current thread is switched to another process; the sleep (int millsecond) method is executed to make the current process enter the sleep state; the wait() method is executed to make the current process enter the waiting state; an operation is executed to enter the blocking state.
    4. Die: Ends the run() method.

Thread class and thread creation

  • Using the Thread class:
    1. Give the target object of the thread object and implement the Runnable interface.
    2. Create a thread object and specify its target object.
    3. The relationship between the target object and the thread: complete decoupling, weak coupling
    4.run() method

Thread common methods

  • start(): Calling the start() method again will cause an exception without ending the run() method.
  • run()
  • sleep (int millsecond)
  • isAlive()
  • currentThread(): Returns the CPU resource currently being used.
  • interrupt(): Wake up the thread that is sleeping.
  • Note: Do not create new objects while the thread is in use.

thread synchronization

  • Several threads need to use a synchronized method.
  • Synchronization mechanism: When a thread uses a synchronized method, other methods can only wait for the thread to finish using the method before using it. (Sync method cannot be started 2 times)

Coordinate synchronization process

  • The wait() method can interrupt the execution of the thread and release the right to use the CPU.
  • Use the notifyAll() method to notify all threads that are waiting due to the use of synchronization methods to end waiting, and follow the principle of "interrupt first, continue first".
  • Note: wait(), notify() method and notifyAll() method cannot be used in non-synchronized methods and cannot be overridden.
    The -wait() method should be placed in a "while(wait for condition){}" loop, not an "if(wait for condition){}" branch statement.

thread union

  • While a thread occupies the CPU, other threads can call join() to join this thread.

GUI thread

-AWT-EventQuencue thread is responsible for handling GUI events, AWT-Windows is responsible for drawing forms or components to the desktop.

timer thread

  • The thread created by the Thread class is called a user thread, and the Timer class creates a thread for special timing.
  • The start() method starts the timer; stop() stops the timer, that is, suspends the thread; restart() restarts the timer, that is, resumes the thread.

Daemon thread

  • Threads are non-daemon threads by default, also known as user threads. When all user threads in the program have finished running, even if the daemon thread still has statements to execute, the daemon thread will immediately end execution.
  • A thread can call void setDaemon(boolean on)methods to set itself up as a daemon thread.
  • A thread must set itself to be a daemon thread before running.

Problems and Solving Processes in Teaching Materials Learning

  • Question 1: What is thread synchronization?
  • Solution to problem 1: Several threads need to call the same synchronization method (method decorated with synchronized).
  • Question 2: In what state is the thread calling isAlive() and the return value is false?
  • Solution to problem 2: When the thread is in the new state, the thread calls the isAlive() method and the return value is false.

code hosting

Summary of last week's exam mistakes

  • Wrong question 1: The following are commonly used databases are ACDE
    A . Access
    B . XAMMP
    C . MySQL
    D . Oracle
    E . SQL Server

  • Wrong question 2: The interface RowSet inherits the interface __, and the method __ in RowSet can be called to fill its data area. C
    ①Statement ②ResultSet ③update ④populate
    A . ①③
    B . ①④
    C . ②③
    D . ②④

Others (perception, thinking, etc., optional)

Since this chapter is a difficult point in Java, and there is a lot of knowledge that is difficult to understand, I spent more time through code, video tutorials and textbooks to learn a lot about multi-threading in Java, and I have to practice reading books after class to consolidate knowledge.

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 37/37 1/1 20/20
the second week 654/691 3/4 18/38
The third week 477/1131 3/7 22/60
the fourth week 657/1730 2/9 30/90
fifth week 1260/2515 1/10 30/120
Week 6 1022/3319 2/12 20/140
Week 7 1213/3803 1/13 20/160
eighth week 993/4796 4/17 30/190

References

Guess you like

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