20165203 "Java Programming" Eighth Week Learning Summary

20165203 "Java Programming" Eighth Week Learning Summary

Textbook learning content summary

Chapter 12

  • process and thread
    • The completion process of the process: code loading, execution to completion
    • Threads: A process consists of multiple threads.
    • The completion process of the thread: its own birth, existence and death
    • Relation: Without processes there can be no threads
    • Threads are small threads running in a process.
  • thread
    • Multithreading: A process in which several executors jointly execute a program.
    • Note: The computer can execute only one of those threads at any given moment.
    • Establish a multi-threading mechanism: The Java virtual machine quickly switches control from one thread to another. These threads are executed in turn, and each thread has the opportunity to use CPU resources.
    • Main thread: The thread started after the main method is found when the JVM loads the code
    • Thread's state:
      new
      running: using start()method
      interrupted
      dead

  • There are two ways to create a thread :
  1. Use Threadclass subclass to create: need to override run()method.
  2. Using a Threadclass
    constructor: You Thread(Runable target)
    need to create an Runnableinstance of an interface class and override runmethods in that interface.
  • The relationship between target objects and threads: complete decoupling and target object composition threads
  • Common methods in threads:
  1. start(): Note that start()it is not necessary to let the thread call the method after using the start()method.
  2. run(): Threadoverridden in subclasses run()or Runnable接口written inrun()
  3. sleep(int millsecond): Sleep the process.
    Note: The parameter millsecond is the sleep time in milliseconds. It
    must be try-catchcalled in the statement block sleep方法
    4. isAlive(): The thread does not enter the dead state, the calling isAlive()method returns After true
    the thread enters the dead state, the calling isAlive()method returns false
    5. currentThread(): Returns the thread that is using the CPU thread.
    6. interrupt(): Release the sleep state of the thread.
  • Thread synchronization: Several threads in the program need to use a method, and this method is synchronizeddecorated with.
  • Thread synchronization mechanism: When thread A uses a synchronizedmethod, other threads synchronizedmust wait until thread A finishes using the synchronizedmethod.
  • Threads that coordinate synchronization:
    1. wait(): Used in the synchronization method to make the thread wait and temporarily give up the right to use the CPU.
    2. notifyAll(): Notify all threads that are waiting due to the use of this synchronization method to end waiting, and the thread that has been interrupted will continue to execute this synchronization method from the point where it was interrupted just now.
    3. notify(): Notify that one of the waiting threads has finished waiting.
    ++ must use these three methods in synchronized methods ++

  • Thread association
    join(): other threads call this method to associate with threads that are using CPU resources.

  • GUI thread
  • Timer thread: useTime类
  • Daemon thread: how to use void setDaemon(boolean on)it

Problems and Solving Processes in Teaching Materials Learning

Q1: When studying the textbook, did you forget the previous knowledge and don't know the specific meaning of JVM?

A1: After consulting the textbook and searching the Internet, I got a more comprehensive answer.
JVM is actually the abbreviation of Java Virtual Machine. It is the key for Java programs to achieve their ++ platform independence. It is precisely because of Java's platform independence that Java language compilers only need to generate object code that runs on the Java virtual machine. (bytecode), it can run unmodified on a variety of platforms.

Q2: When learning as shown in the figure, currentThread()questions were raised about the method in the program.
A2: In fact, when I learned to the back, the currentThread()method was explained in the textbook. Here I will talk about my own understanding. The currentThread()method is Threada method in the class, which is used to return the thread that is currently using CPU resources, and getName()use it together with .

Problems and solutions in code debugging

Q1: When running the program Example12_9.java, the running result is as shown in the figure, which is inconsistent with the result given in the example of the textbook.

A1: Read the textbook carefully and find that catMakerthe name given to the thread in the textbook is wrong, as shown in the figure.

code hosting

Summary of last week's exam mistakes

Q1: 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 . ②④

A1: Correct solution: C
Analysis: Compared with ResultSet,
(1) RowSet extends ResultSet interface, so RowSet can be used like ResultSet.

  (2) RowSet extends the ResultSet interface, so it has more and richer functions than ResultSet.

  (3) By default, all RowSet objects are scrollable and updatable. The ResultSet is scroll-forward only and read-only.

  (4) RowSet can be non-linked, and ResultSet is connected. Therefore, the data can be manipulated offline using the CacheRowSet interface.

  (5) The RowSet interface adds JDBC API support to the JavaBeans component model. A rowset can be used as a JavaBeans component in the Visual Beans development environment.

  (6) RowSet adopts a new method of connecting to the database.
  update is often used to update data.

Others (perception, thinking, etc., optional)

This week's Java study mainly learned about threads, which seems to have opened the door to a new era. I feel that threads are very interesting, and I am trying to write some interesting programs with threads. In addition, through this week's study, I also found that the previous knowledge is somewhat forgotten, and I have to seize the time to find out the omissions and fill the gaps.

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 48/48 1/1 20/20
the second week 390/438 2/3 18/38
The third week 886/1304 1/4 18/56
the fourth week 1105/2409 1/5 30/86
fifth week 1229/3196 1/6 20/106
Week 6 1147/4343 2/8 25/131
Week 7 1862/3668 1/9 20/151
eighth week 955/5298 1/10 20/171

Guess you like

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