20165218 2017-2018-1 "Java Program Design" 8th Week study Summary Textbook Learning summary Chapter 12th Java multithreading mechanism threads in Java
- a computer can execute only one thread at any given time , and a multithreaded mechanism is a way for a Java virtual machine to quickly switch threads and make them take turns
Thread
The object representing the class and its subclasses represents the thread
- One life cycle of a thread has four states of new, running, Interrupted, dead
setPriority(int grade)
method to adjust Thread priority
Common Methods of threading
start()
Only new state threads can call the
run()
Without specific content, the user program needs to create its own subclass of the thread class, overriding the run()
method; after execution, the thread dies
sleep(int millsecond)
Call causes a high-priority thread to temporarily enter hibernation; try-catch
isAlive()
When the thread is in the new and dead state, the method returns false
and is in a running state, returningtrue
currentThread()
Returns the thread that is using CPU resources
interrupt()
The thread used to "wake up" The method was called sleep
Thread synchronization
Using the synchronized -Modified method, you follow the synchronization mechanism: When a thread a uses the Synchronized method, other threads must wait until thread A is finished using the method.
Reconciling threads for synchronization
wait()
Method can be executed in the thread break, temporarily let the CPU use
wait()
Method must be notifyAll()
awakened by method
wait()
method, notify()
which notifyAll()
is the final method in the object class , is not allowed to be overridden
Thread Federation
//线程A在使用CPU资源的期间B.join();
A interrupts immediately until thread B finishes executing
Code Hosting
(run result of statistics.sh script)
Last week's summary of the wrong quiz
Connection is the class name is not a method name
Resources
The 8th Week of Java Programming Study summary 20165218 2017-2018-1