Multithreading: Multiple execution units
Process: Running Application
Threads: The execution unit of a process, an execution path
Thunder Download, 360 cleanup
Multithreaded Program implementation:
Threads are dependent on process existence, thread class
Subclass Inheritance
Subclasses overriding the Run method
Call Strart (), let the thread start, call the Run () method
The same thread object started twice in a row, error: Indicates a problem with the thread state
GetName () Method: Flags different threads, gets the thread name
SetName (): Change thread name
Thread Randomness:
Program execution, preemption CPU resources, CPU switching between multiple programs.
The second implementation:
Create a class to implement the Runnable interface
Overriding the Run method
To create an instance of a class
Create a thread object reference by taking the implementation of the class as the thread's construction parameter
Thread.CurrentThread (). GetName ()
Advantage:
Avoids the limitation of single inheritance;
The way to implement the interface is created only once, better to achieve the separation of data and operations.
Ticket code:
A: Sharing of data; two ways;
B: Sell negative, sell the same ticket-solve the problem way
"The randomness and latency of threads"
Find the source of the problem:
A: See if there is any data sharing;
B: Look at the operation of shared data is not a number of statements;
C: See if it's in a multithreaded program
FIX: Lock the code that satisfies the condition together synchronized
Lock object: The lock of the synchronization code block is any object
Synchronization method: Lock on Method---thread safety problem
This object
Static method: Class name. Class class "Bytecode file" type Object
Dead lock
The less code that is synchronized, the better it is, it is recommended to use a synchronous code block
If a method is all synchronized, use the synchronous method
Inter-thread communication:
Student information input and output: multithreading randomness
Input one, output one---the same
Student Information Entry information Acquisition centralized test class
Java Basics-Day 23rd