当前位置:网站首页>Thread synchronization, life cycle

Thread synchronization, life cycle

2022-04-23 14:45:00 clear0217

Thread synchronization

So called synchronization , When a function call is issued , Before we get results , The call does not return , At the same time, other threads cannot call this method .

  1. The need for thread synchronization

Ensure multi-threaded secure access to competing resources .
To prevent multiple threads from accessing a data object , Damage to data .

  1. Thread synchronization principle

In the above example, we can see that when we implement thread synchronization , Used synchronized keyword . The keyword modification is equivalent to locking the method . When a thread accesses a method, it needs to obtain a lock first , Then the method can be executed . here , If other threads come , Found that the lock has been occupied , Wait at the method call , Until the lock is released , Then compete for the lock . Gets the lock , Then execute the method , otherwise , Continue to wait for .

Thread lock

 Every Java Object has a built-in lock , There is one and only one .
 When the program executes to a non static synchronized Synchronizing methods ,( If the lock is not occupied by other threads ) Automatically obtain the lock of the object to which the method belongs .
 It's not just a method that can be modified to synchronize methods , You can also lock code blocks .
 When the program is executed , The lock is released when the synchronization block or synchronization method is exited .

【 Be careful 】

 You don't have to synchronize all the methods in the class , Synchronize as needed . 
 Threads sleep When sleeping , The lock it holds will not release . 
 Threads can acquire multiple locks . 
 Synchronization damages concurrency , You should minimize the scope of synchronization .

Lock
Lock and synchronized To solve the problem of thread synchronization ,Lock be relative to synchronized More functions 、 More flexible use 、 More object oriented .

【 Use 】

private Lock lock = new ReentrantLock();
public void take(){
    
    lock.lock();
    // The synchronization part 
    lock.unlock();
}

There are five states in the life cycle of a thread : newly build 、 be ready 、 function 、 Blocking 、 Death

 newly build : use new Statement creation complete  
 be ready : perform start after  
 function : Occupy CPU Time  
 Blocking : Yes wait sentence 、 Yes sleep Statement and waiting for an object lock , Waiting for input  
 End : After execution run() Method 

 Insert picture description here

The difference between a process and a thread

Processes have separate code and data spaces , The cost of inter process switching is high .
Multiple threads of the same process share process code and data space , Only separate thread stacks are retained 、 Program counter , Low switching cost .
Multiprocesses are multiple independent tasks that can be seen running in parallel in the operating system .
Multithreading refers to multiple parallel executing programs in the same process .

版权声明
本文为[clear0217]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231426296186.html