thread
Process ----- > program in progress
Thread ----- > a process can create multiple threads
Concurrency: multiple tasks are executed alternately at the same time, resulting in an illusion that they appear to be carried out at the same time. In short, multitasking of a single cpu is concurrency
Parallel: parallel can be achieved by executing multiple tasks at the same time and multiple CPUs
Basic use of threads
Inherit Thread class
Implement Runnable interface
It shows that Java is a single inheritance mechanism. In some cases, a class may have inherited a parent class. At this time, it is impossible to create a Thread by inheriting the Thread class method, so you can create a Thread by implementing the Runnable interface
Common methods group II
The comity of the yield thread is not necessarily successful
Queue jumping of join thread once the queue jumping thread succeeds, it must first complete all the tasks of the inserted thread
A user thread is also called a worker thread. When the task of the thread is completed or the notification method ends
Daemon threads generally serve worker threads. When all user threads end, daemon threads automatically end
Common daemon threads: garbage collection mechanism
Thread life cycle
NEW threads that have not been started are in this state
RUNNABLE the thread executing in the Java virtual machine is in this state (READY RUNNING)
BLOCKED the thread waiting for the monitor to lock is in this state
WAITING the thread that is WAITING for another thread to perform a specific action is in this state
TIMED_WAITING is waiting for another thread to execute the action. The thread that has reached the specified waiting time is in this state
TERMINATED exited threads are in this state
Thread synchronization
In multithreaded programming, some sensitive data are not allowed to be accessed by multiple threads at the same time. At this time, synchronous access count is used to ensure that data can be accessed by at most one thread at any one time to ensure data integrity
Understand thread synchronization, that is, when a thread is operating on memory, no other thread can operate on the memory address. Only when the thread completes the operation can other threads operate on the memory address
Synchronization specific method
1. Synchronization code block
Synchronized (object){
Get the lock of the object to operate the synchronization code
}
2. Synchronization method
public synchronized void m(String name) {
}
mutex
introduce
- The concept of object mutex is introduced into Java to ensure the integrity of shared data operations
- Each object corresponds to a tag called "mutex", which is used to ensure that only one thread can access the object at any time
- Keyword synchronized to associate with the mutex of an object. When an object is decorated with synchronized, it indicates that the object can only be accessed by one thread at any time
- The limitation of synchronization leads to the reduction of program execution efficiency
- The lock of a synchronous method (non static) can be this or another object
- The lock of the synchronous method (static) is the current class itself
If the synchronization method does not use static modification, the default lock object is this
If the method uses static to decorate the current class of the default lock object class
Thread deadlock
Multiple threads occupy each other's lock resources and refuse to yield, resulting in deadlock
Simulate thread deadlock
Click to view the codepublic class DeadLock_ { public static void main(String[] args) { DeadLockDemo A = new DeadLockDemo(true); A.setName("A thread "); DeadLockDemo B = new DeadLockDemo(false); B.setName("B thread "); A.start(); B.start(); } } @SuppressWarnings({"all"}) class DeadLockDemo extends Thread { static Object o1 = new Object();// Ensure multithreading and share an object. Here, use static static Object o2 = new Object(); boolean flag; public DeadLockDemo(boolean flag) {//constructor this.flag = flag; } @Override public void run() { //The following is the analysis of business logic //1. If the flag is T, thread A will first obtain / hold the o1 object lock, and then try to obtain the o2 object lock //2. If thread A cannot get o2 object lock, it will be Blocked //3. If the flag is F, thread B will get / hold the o2 object lock first, and then try to get the o1 object lock //4. If thread B cannot get the o1 object lock, it will be Blocked if (flag) { synchronized (o1) {//Object mutex, the following is the synchronization code System.out.println(Thread.currentThread().getName() + " Enter 1"); synchronized (o2) { // Here you get the monitoring right of the li object System.out.println(Thread.currentThread().getName() + " Enter 2"); } } } else { synchronized (o2) { System.out.println(Thread.currentThread().getName() + " Enter 3"); synchronized (o1) { // Here you get the monitoring right of the li object System.out.println(Thread.currentThread().getName() + " Enter 4"); } } } }
Release lock
The following operation will release the lock
1. The execution of the synchronization method synchronization code block of the current thread ends
2. The current thread encountered a break return in the synchronization code block synchronization method
3. The current thread has an unhandled Error or Exception in the synchronization code block synchronization method, resulting in an abnormal end
4. The current thread executes the wait method of the thread object in the synchronization code block synchronization method. The current thread pauses and releases the lock
The following operation will not release the lock
1. When a thread executes a synchronization code block or synchronization method, the program calls thread sleep() Thread. The yield () method pauses the execution of the current thread and does not release the lock
2. When a thread executes a synchronous code block, other threads call the suspend() method of the thread to suspend the thread. The thread will not release the lock
This method is no longer recommended