20165218 2017-2018-1 "Java Programming" Week 8 Learning Summary
Textbook learning content summary
Chapter 12 Java Multithreading Mechanisms
threads in java
- A computer can only execute one thread at any given time , and the multi-threading mechanism refers to the Java virtual machine quickly switching threads so that they execute in turn
Thread
Objects of class and its subclasses represent threads- A thread's life cycle has four states : new, running, interrupted, and dead
setPriority(int grade)
method to adjust thread priority
Common methods of threading
start()
Only threads in the newly created state can callrun()
There is no specific content, the user program needs to create a subclass of its own Thread class and override therun()
method; after execution, the thread diessleep(int millsecond)
Called to temporarily put a high-priority thread to sleep; must betry-catch
used inisAlive()
The method returns when the thread is in the new and dead state, and returnsfalse
when it is in the running statetrue
currentThread()
Returns the thread that is using CPU resourcesinterrupt()
Used to "wake up"sleep
the thread that called the method
thread synchronization
The method of using synchronized modification must comply with the synchronization mechanism: when a thread A uses the synchronized method, other threads must wait until thread A is finished using this method.
coordinating synchronized threads
wait()
The method can interrupt the execution of the thread and temporarily give up the right to use the CPUwait()
method mustnotifyAll()
wake up with methodwait()
Methods,notify()
,notifyAll()
are final methods in the Object class and are not allowed to be overridden
thread union
//线程A在使用CPU资源的期间
B.join();
A interrupts immediately until thread B finishes executing
code hosting
(screenshot of the running result of the statistics.sh script)
Summary of last week's exam mistakes
- wrong question 1
Connection is a class name not a method name
wrong question 2
...