20165230 2017-2018-2 "Java Programming" Week 8 Learning Summary
Textbook learning content summary
Chapter 12 Java Multithreading Mechanism
- A process can spawn multiple threads during its execution. A thread is a smaller unit of execution than a process
- The JVM guarantees that each thread has a chance to use CPU resources and waits until all threads in the Java application have ended before ending the application
Thread state and life cycle
- Representing threads using
Thread
objects of class and its subclasses - Call
start()
the method to join the thread to the JVM managed queue. After the call, the thread can no longer call thestart()
method run()
The method specifies the specific task of the thread. The thread becomes dead after the method is executed.
-sleep(int millsecond)
Put the thread to sleep. Threads with high priority can give up CPU resources to threads with low priority.wait()
method puts the thread into a wait state.notify()
Continue the thread from where it left off by means of a method. It is common to putwait()
methods in a loop of "while"
Common method
isAlive()
Used to determine whether the thread is in a new state or in a dead state, if so, return false.- An already running thread does not assign entities to the thread without entering the dead state
currentThread()
Returns the thread currently using CPU resourcesinterrupt()
Used to "wake up"sleep()
the sleeping thread that called the methodThread class and thread creation
- When writing subclasses, you need to override
run()
methods The constructor for creating a thread:
Thread(Runnable target)
, the parameter is an interface of Runnable type, and an instance object needs to be passed to the interfacethread synchronization
- Thread synchronization mechanism: when a thread uses a
synchronized
method, if other threads want to use it, it mustwait()
wait for the method until the thread finishes using the method - If other threads do not need to wait when using the synchronized method, when the method is used up, the execution
notifyAll()
method notifies all the threads in the waiting state to end the waiting. Cannot use wait(), notify(), notifyAll() methods in asynchronous methods
thread union
The B thread can be
B.join()
federated by the A thread. Thread A will immediately interrupt execution, and resume execution after waiting for B to finish executingGUI thread
- 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(int a,Object b)
Create a timerstart()
start timerstop()
stop timerrestart
restart timerDaemon thread
- Call
void setDaemon(boolean on)
method to set daemon thread A thread must set whether it is a daemon thread before running
Problems and Solving Processes in Teaching Materials Learning
- Question 1: What is the difference between the two ways to implement multithreading in java?
Problem 1 solution: learned through blog
By inheriting the Thread class:
- (1) Advantages: It is simple to write. If you need to access the current thread, you don't need to use the Thread.currentThread() method, you can use this directly to get the current thread.
- (2) Disadvantage: Because the thread class has inherited the Thread class, it cannot inherit other parent classes.
- By implementing the Runnable interface:
- (1) Advantages: The thread class only implements the Runable interface and can also inherit other classes. In this way, multiple threads can share the same target object, so it is very suitable for multiple same threads to process the same resource, so that the CPU code and data can be separated to form a clear model, which better reflects the Object-oriented thinking.
- (2) Disadvantage: The programming is slightly complicated. If you need to access the current thread, you must use the Thread.currentThread() method.
- Question 2: Why is the code of p377 TicketHouse
wait()
changed toThread.sleep(3000)
, Li Kui will never be able to buy tickets? Problem 2 solution: By comparing the wait() method and the Thread() method, we learned that:
In the process of calling the sleep() method, the thread does not release the object lock.
When the wait() method is called, the thread will give up the object lock and enter the waiting lock pool waiting for this object. Only after calling the notify() method on this object will the thread enter the object lock pool to prepare to
acquire the object lock and enter the running state.
Problems and solutions in code debugging
- Question 1: When debugging example12_3, why is the result that the dog drinks 7 and the cat drinks 8?
- Problem 1 Solution: Through learning the teaching material video, it is known that the running results of each computer are not the same, and it depends on the current CPU resource usage since the output of the results.
Summary of last week's exam mistakes
wrong question 1
- select is also a data manipulation language
wrong question 2
The original words in the book: "The command line window occupied by the MySQL database server cannot be closed directly, but the MySQL database server can be closed by using the task manager"
code hosting
https://gitee.com/BESTI-IS-JAVA-2018/20165230/commit/5dd42538867b45b2b037b6d2039c2cf622fd26d9
Code screenshot
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 | 13/13 | 1/30 | 19/19 | |
the second week | 426/426 | 3/30 | 12/12 | |
The third week | 562/562 | 4/30 | 15/15 | |
the fourth week | 1552/1958 | 5/30 | 16/16 | |
fifth week | 1123/3086 | 6/30 | 14/14 | |
Week 6 | 747/3833 | 8/30 | 12/12 | |
Week 7 | 953/4786 | 11/30 | 13/3 | |
eighth week | 4564/5881 | 13/30 | 14/14 |