Multithreaded programming learning II (concurrent access to objects and variables)
1、 Concept
2、 Synchronized synchronization method
1. The problem of non thread safety exists in the instance variable. If the variable is a private variable inside the method, there is no problem of "non thread safety" and it is always thread safe. This is caused by the fact that the variables inside the method are private@ H_ 301_ 15@
@H_ 301_ 15@@H_ 301_ 15@
@H_ 301_ 15@
2. If the class instance variable is accessed and the method is not synchronized, multiple threads will mistakenly modify the same variable value, resulting in thread insecurity. This problem has been mentioned in the previous blog post@ H_ 301_ 15@
3. The method calling the keyword synchronized declaration must be queued. In addition, we need to firmly remember the word "sharing". Only the read-write access of shared resources needs to be synchronized. If it is not shared resources, there is no need for synchronization at all. In other words, if different threads do not access the same instance variable, there is no thread contention for resources. Where is the thread unsafe problem? So there is no need to synchronize@ H_ 301_ 15@
4. The lock of the synchronized method is held by the instance object of this class, that is, the same lock actually held by different synchronized methods in an object object belongs to the instance of the object: @ h_ 301_ 15@
3、 Synchronized synchronized statement block
1. The synchronized synchronization method has some disadvantages. The part of code that does not operate on instance variables in the synchronized synchronization method also needs thread waiting and locking mechanism. However, for the program, the completion of that part of the code can be executed asynchronously, reduce the waiting time and improve the running efficiency. In this way, there is a synchronized synchronous statement block@ H_ 301_ 15@
2. When two concurrent threads access the synchronized (this) synchronized code block in the same object object, only one thread can be executed for a period of time, and the other thread must wait for the current thread to execute the code block before executing the code block@ H_ 301_ 15@
@H_ 301_ 15@@H_ 301_ 15@
3. Like the synchronized method, the synchronized (this) code block locks the current object. Of course, Java also supports the function of synchronizing "any object" as a lock object. Most of the "arbitrary objects" are instance variables and method parameters. The format used is synchronized (not this object)@ H_ 301_ 15@
Advantages of locking non this objects: if there are many synchronized methods in a class, synchronization can be realized, but it will be blocked, so the operation efficiency will be affected; However, if a synchronous code block is used to lock a non this object, the programs and synchronization methods in the synchronized (non this) code block are asynchronous and do not compete with other synchronization methods to lock this object, which can greatly improve the operation efficiency@ H_ 301_ 15@
4. The basis for judging whether a multithread executes synchronized synchronously or asynchronously is: (as long as the object does not change, even if the object's properties are changed, the running results are synchronized.) @ h_301_15@
5. The keyword synchronized can also be applied to static static methods, so that the current * The class class corresponding to the java file is locked. This can refer to my blog@ H_ 301_ 15@
6. Pay special attention to the function of string constant pool cache, because two string objects may refer to the same memory space. Therefore, in most cases, the synchronized code block does not use string as the lock object, but uses others, such as new object() to instantiate an object object, but it is not put into the cache@ H_ 301_ 15@
7. Deadlock should be avoided in the program. The reason for deadlock is that there is cross reference between locks. Both threads are waiting for each other to release the lock: @ h_ 301_ 15@
@H_ 301_ 15@@H_ 301_ 15@
4、 Volatile keyword
1. In multithreading, there is a problem that the values in the private stack are out of sync with those in the public stack. What do you mean? It is possible that the thread modified the value of the variable in memory in one place, while elsewhere the thread read the inconsistent variable value from the private stack@ H_ 301_ 15@
@H_ 301_ 15@@H_ 301_ 15@
@H_ 301_ 15@
2. The keyword volatile can be used to modify a field (member variable), which tells the program that any access to the variable needs to be obtained from the main memory, and its changes must be flushed back to the main memory synchronously. It can ensure the visibility of variable access by all threads. @ h_301_15@
@H_ 301_ 15@
@H_ 301_ 15@@H_ 301_ 15@
3. What is the difference between synchronized and volatile@ H_ 301_ 15@
4. Reason for non thread safety of keyword volatile: @ h_ 301_ 15@
@H_ 301_ 15@@H_ 301_ 15@
In a multithreaded environment, use and assign occur many times, but this operation is not atomic. That is, after read and load, if the count variable in the main memory is modified, the value in the thread working memory will not change because it has been loaded, that is, the variables of private memory and public memory are not synchronized, Therefore, the calculated results are different from expectations, which leads to the problem of non thread safety@ H_ 301_ 15@
For variables decorated with volatile, the JVM virtual machine only ensures that the value loaded from the main memory to the thread working memory is the latest. For example, when thread 1 and thread 2 find that the value of count in the main memory is 5 during read and load operations, name will load the latest value. In other words, the volatile keyword solves the visibility problem of variable reading, However, atomicity cannot be guaranteed. Lock synchronization is still required for multiple threads to access the same instance variable@ H_ 301_ 15@
5. In addition to using the synchronized keyword, you can also use the atomicinteget atomic class to achieve synchronization. However, in the case of logic, atomic classes are not completely safe because although the methods of atomic classes are atomic, the calls between methods are not atomic (synchronized is still required at this time)@ H_ 301_ 15@
6. The keyword synchronized not only enables multiple threads to access the same resource synchronously, but also has the function of synchronizing private variables in thread working memory with variables in public memory. It contains two characteristics: mutual exclusion and visibility. Synchronized essentially acquires the monitor of an object, and this acquisition process is exclusive, that is, only one thread can obtain the monitor of the object protected by synchronized at the same time. (any object has its own monitor) @ h_301_15@
@H_ 301_ 15@@H_ 301_ 15@
7. According to the "happen before" principle of the JAVA memory model, the write operation to the volatile field precedes the read operation. Even if two threads modify and obtain the volatile variable at the same time, the get operation can get the latest value@ H_ 301_ 15@
8. To learn multithreading concurrency, we should focus on "external practice is mutually exclusive, and internal practice is visible"@ H_ 301_ 15@