20165233 2017-2018-2 "Java Programming" Week 8 Learning Summary
Textbook learning content summary
Basics: Threads in Java, the Thread class and the creation of threads
- A thread is a smaller unit than a process.
- The thread scheduler in the JVM (Java Virtual Machine) is responsible for managing threads. In a system that uses time slices, each thread will have the opportunity to obtain the right to use the CPU.
- After the thread is created, it only occupies the memory resources. There is no such thread in the threads managed by the JVM. This thread must call a
start()
method to notify the JVM.
Key points (difficulties): thread synchronization, coordinating synchronized threads
- Thread synchronization means that several threads need to call the same synchronization method.
- When a thread uses a synchronization method, it may be necessary to use the
wait()
method to temporarily give up the use of the CPU according to the needs of the problem. - If other threads do not need to wait when using this synchronization method, then when it uses up this synchronization method, it should execute a
notifyAll()
method to notify all threads that are waiting due to using this synchronization method to end waiting.
- Understand: Common Methods of Threads, Thread Federation, GUI Threads
Practical: Counter Threads
Problems and Solving Processes in Teaching Materials Learning
- Question 1: Can there be more than 2 infinite loops in a Java application?
- Solution to Problem 1: The above problem cannot be solved without using multi-threading technology. Because the first code
while
loops infinitely in the statement, the code in the secondwhile
statement never gets a chance to execute. If two threads can be created in the main thread , then the twowhile
statements will be executed alternately. - Question 2: Why does the program have to override the methods of the parent class in the child class
run()
? - Solution to problem 2: The reason is that the
run()
method in the Thread class has no specific content, and the program needs to override the method in the subclass of the Thread class to overriderun()
the method of the parent classrun()
. - Question 3: What is a time slice?
- Solution to problem 3: Time slice is the time that the CPU allocates to each program, each thread is assigned a time period, called its time slice, that is, the time the process is allowed to run, so that each program appears to be running at the same time. of.
Problems and solutions in code debugging
Question 1: In Example12_1.java on page 362 of the textbook, the main thread executes the first loop and outputs
, Why did the main thread not finish executing the for loop statement?主人1
Solution to problem 1: Since the main thread is using CPU resources, it has already executed
At this point, the JVM already knows that there are 3 threads. Therefore, after the main thread uses CPU resources to execute the first loop of the for statement, the JVM switches the CPU resources to the speakCar thread.speakElephant.start(); speakCar.start();
Question 2: In Example12_2.java on page 366 of the textbook, why is the
speakCar.start();
remove, or change to
When the JVM thinks there are only two threads in the program?carTarget.run();
Solution to problem 2:
start()
When the method is to create a new thread, if it is removed, it is considered that there is no such thread;carTarget.run();
it may be interrupted during the running process and become the code of the main thread, and the JVM only switches between the main thread and the Elephant thread.
code hosting
Summary of last week's exam mistakes
- Error 1: The interface RowSet inherits the interface ResultSet, and the method update in RowSet can be called to fill its data area.
Reason: The method update in RowSet can be called to fill its data area, and update is filled with populate. The function of the method in RowSet is not clear.
Understanding: The method is understood.
- Error 2: The type returned by executeUpdate is int, which means the affected records. true
Reason: Not clear about executeUpdate.
Understanding: The method is understood.
- Wrong question 3: The following are data manipulation languages: A.insert and B.update
Reason: unclear understanding of data manipulation language. Select is also selected as the data manipulation language.
Comprehension: Have mastered the basic content of data manipulation language.
Others (perception, thinking, etc., optional)
The 12 chapters of this week's learning content have a deeper understanding of threads. Before learning, the concept of thread and process was very confusing, and now it is more clear. In the process of learning the content of the textbook, the thread is closer to the operation of some computer applications. It is not like the very straightforward program statements in the previous chapters. It is only aimed at a program that solves the topic of the textbook. This chapter is more conducive to solving the actual program. The problem of running is more abstract and more practical.