1. Multithreading
1.1. Processes and Threads
? A process is a running program.
? There can be multiple threads in a process, and threads are the basic unit of CPU scheduling and dispatch. We can understand that a thread is a path in a program's run.
1.2. Creation and use of multithreading
1.2.1. creating
Customizing a class to inherit the thread class or implement the runnable interface
1.2.2: Two different ways to create multithreading
Inheriting the thread class: Thread() or Thread(String name) multiple threads completing their own tasks individually
Implement the Runnable interface: Thread(Runnable target) or Thread(Runnable target, String name) multiple threads to complete a task together
1.2.3: Thread Start-up
? Both of these are created by invoking the start () method of the Thread object. When the start () method is called, the CPU opens a new thread and executes the run () method on the new thread.
1.2.4: Thread Common methods
? CurrentThread: Static method used to get the current thread
? GetName, SetName: Used to get, set the name of the current thread
? Sleep: Control thread hibernation in milliseconds
? Setdaemon: Sets the thread as the daemon thread. The thread defaults to a non-daemon thread, and the daemon thread cannot execute it alone.
? Join: The current thread is paused, the thread waiting to join ends, and the current thread continues execution.
1.3: Synchronization of Threads:
? Synchronizing code block synchronized (lock object ) {code to be synchronized ... } Form locks the code that accesses the data, and only one thread executes the content in the synchronization code block at the same time.
Method Lock: For example: public synchronized void Testsyn () {}. The synchronous non-static method uses this as the lock, and the static method uses the class object itself
1.4: The life cycle of a thread:
1. New State (new): The thread object created with the new statement is in the new state, which, like other Java objects, is only allocated memory in the heap
2. Ready state (Runnable): When a thread is created, the other thread calls its start () method, and the thread enters the ready state. Threads in this state are in a running pool, waiting for the CPU to use
3. Running state (Running): Thread in this state consumes CPU, code executing program
4. Blocking state (Blocked): When a thread is in a blocking state, the Java Virtual Machine does not allocate the CPU to the thread until the thread is back in the ready state before it has the opportunity to go to the running state.
The blocking state is divided into three cases:
1), blocking state in the object waiting pool: When the thread is running, if the Wait () method of an object is executed, the Java virtual machine puts the thread back into the waiting pool of the object
2), blocking state in the object lock, when the thread is in the running state, trying to obtain a synchronization lock of an object, if the object's synchronization lock is already occupied by other threads, the JVM will put this thread into this object's pool.
3), other blocking states: The current thread executes the sleep () method, or calls the join () method of another thread, or when an I/O request is made, it enters this state.
1.:5: Communication between multiple threads:
The wait () method of the lock object can be used in the synchronization code to let the current thread wait
Use the Notify () method of the lock object to wake the waiting thread
? If multiple threads are waiting, notify () wakes up randomly one
The Notifyall () method can wake up all the waiting threads
2. Locking mechanism
3. The visibility of the memory:
3.1. The visibility of shared variables between threads
Shared variables: If a variable has a copy in the working memory of multiple threads, then this variable is a shared variable for these threads.
Visibility: A thread's modification of shared variable values can be seen by other threads in a timely manner.
Java memory Model (Jmm:java): Describes the access rules for various variables in Java programs, as well as the underlying details of storing variables into memory and reading variables from memory in the JVM.
All variables are stored in the main thread
Each thread has its own independent working memory, which holds a copy of the variable used by the thread (a copy of the variable in main memory)
Features: 1. All operations of a thread on a shared variable must be made in its own working memory and cannot be read and written directly from main memory
2. Variables in the working memory of other threads cannot be accessed directly between different threads, and the transfer of variable values between threads needs to be done through main memory.
2.2.java language-level support for visibility implementations (synchronized, volatile):
3.2.1: Instruction reordering: Code is written in a different order from the actual execution, and the reordering of instructions is the compiler or processor's optimization to improve program performance
2.1: Compiler-optimized reordering (compiler Optimizations)
2.2: Instruction-level parallel reordering (processor optimization)
2.3: Reordering of memory systems (processor optimizations)
3.2:as-if-serial semantics: Regardless of reordering, the results of the program execution should be consistent with the results of the Code order execution
1. JVM compiler, runtime, and processor will ensure Java follows as-if-serial semantics in single thread
2.
3.2.3:synchronized Achieving visibility
3.2.3.1: Characteristics: atomicity, visibility
3.2.3.2: Before line threads unlocked, you must flush the latest value of the shared variable into main memory, and when the line Cheng, the value of the shared variable in the working memory will be emptied
3.2.3.3: Thread executes mutually exclusive code procedure:
1. Obtain the mutex;
2. Clear the working memory;
3. Copy the latest copy of the variable from the main memory to the working memory;
4. Execute the Code;
5 refreshes the current value of the shared variable into main memory;
6 Releasing the mutex lock
3.2.4:volatile Achieving visibility
Order reordering
Volatile use precautions
Synchronized and volatile
Java Basics and Multithreading