20165230 2017-2018-2 "Java Program Design" 8th Week study Summary Textbook Learning contents Summary 12th Chapter Java Multithreading mechanism
- A process can produce multiple threads during its execution. A thread is a smaller execution unit than a process
- The JVM guarantees that each thread will have the opportunity to use CPU resources until all threads in the Java application end before ending the application
Thread state and life cycle
- Using
Thread
objects of classes and their subclasses to represent threads
- Invokes
start()
a method to join a thread to the JVM management queue. The call can no longer be called by the thread to the start()
method
run()
METHOD specifies the specific mission of the thread. The thread becomes a dead state after the method has finished executing.
- sleep(int millsecond)
puts the thread into hibernation. A thread with high priority can let the CPU resources be executed with a low priority thread
wait()
method causes the thread to enter a wait state. notify()
The method enables the thread to continue running from the point of interruption. The method is usually wait()
put into a "while" loop statement
Common methods
isAlive()
Used to determine if a thread is in a new state or into a dead state, and if so, returns false.
- A thread that is already running does not assign an entity to a thread when it does not enter a dead state
currentThread()
Returns the thread that is currently using CPU resources
interrupt()
The thread used to "wake up" the calling sleep()
method to hibernate
Thread class and threading creation
- When writing subclasses, you need to override the
run()
method
To create a thread constructor: Thread(Runnable target)
The parameter is an interface of type runnable, passing an instance object to the interface
Thread synchronization
- Thread synchronization mechanism: When a thread uses a
synchronized
method, other threads must wait with the method if they want to use it wait()
until the thread finishes using the method
- While other threads do not need to wait while using the synchronous method, the execution
notifyAll()
method notifies all waiting threads to end the wait while the method is exhausted
You cannot use the wait (), notify (), Notifyall () methods in a non-synchronous method
Thread Federation
The b thread is available through the B.join()
federated a thread. A thread will break execution immediately and resume execution when B is finished
GUI thread
- Awt-eventquecue thread is responsible for handling GUI events
Awt-windows thread is responsible for drawing a form or component to the desktop
Timer thread
Timer(int a,Object b)
Create a Timer
start()
Start timer
stop()
Stop Timer
restart
Restart Timer
Daemon Threads
- Invoke
void setDaemon(boolean on)
method Settings Daemon Thread
A thread must set itself as a daemon before running
Problems in teaching materials learning and the solving process
- Question 1: What is the difference between the two ways to implement Java multi-threading?
Issue 1 Resolution: Learn through Blogs
Inherited thread classes are used:
- (1) Advantages: Write simple, if you need to access the current thread, without using the Thread.CurrentThread () method, use this directly, you can get the current thread.
- (2) Cons: Because the thread class has inherited the thread class, it is no longer possible to inherit other parent classes.
- Implement the Runnable interface method:
- (1) Advantages: The threading class simply implements the Runable interface and can inherit other classes. In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU code and data can be separated to form a clear model, which is a good embodiment of object-oriented thinking.
- (2) Cons: Programming is slightly more complex, if you need access to the current thread, you must use the Thread.CurrentThread () method.
- Question 2:p377 in the code tickethouse why
wait()
change Thread.sleep(3000)
, Li Kui can never buy tickets?
Issue 2 Resolved: By comparing the Wait () method and the thread () method, you learned:
During the call to the sleep () method, the thread does not release the object lock.
When the Wait () method is called, the thread discards the object lock, enters the waiting lock pool waiting for the object, and the thread only enters the object lock pool when the Notify () method is called for this object.
Gets the object lock into the running state.
Problems in code debugging and the resolution process
- Question 1: When debugging the Example12_3, why the result is the dog drink 7 left, the cat drink 8?
- Issue 1: Through the learning materials video learned that each computer's running results are not the same, the output of the current CPU resource usage depends on
Last week's summary of the wrong quiz
Code Hosting
Https://gitee.com/BESTI-IS-JAVA-2018/20165230/commit/5dd42538867b45b2b037b6d2039c2cf622fd26d9
Code
Learning progress Bar
|
lines of code (new/cumulative) |
Blog Volume (Add/accumulate) |
Learning Time (new/cumulative) |
Important Growth |
Goal |
5000 rows |
30 Articles |
400 hours |
|
First week |
13/13 |
1/30 |
19/19 |
|
Second week |
426/426 |
3/30 |
12/12 |
|
Third week |
562/562 |
4/30 |
15/15 |
|
Week Four |
1552/1958 |
5/30 |
16/16 |
|
Week Five |
1123/3086 |
6/30 |
14/14 |
|
Week Six |
747/3833 |
8/30 |
12/12 |
|
Seventh Week |
953/4786 |
11/30 |
13/3 |
|
Eighth Week |
4564/5881 |
13/30 |
14/14 |
|
Resources
"Java2 Practical Course (Fifth Edition)" Learning Guide
- Comparison of two methods of Java multithreading
The difference between sleep () and wait () in Java
Intellj Idea Easy Tutorial
20165230 2017-2018-2 "Java Programming" 8th Week study Summary