2013年7月30日星期二

JAVA lock

In a concurrent environment, solve shared resource conflicts, you can consider using the locking mechanism.
1. object's lock
All objects are automatically with a single lock.
JVM is responsible for tracking the number of times the object is locked. If an object is unlocked, the count becomes 0. In the task (thread) to lock the object first time, the count becomes 1. Whenever this same task (thread) to acquire a lock on this object, the count is incremented.
Only first obtain a lock tasks (threads) to continue to acquire multiple locks on the object.
Whenever the task leaves a synchronized method, the count is decremented when the count is 0, when the lock is fully released, then other tasks can use this resource.
2.synchronized synchronized block
synchronized in two formats:
Format 1:
synchronized (any object) {
/ / Access to shared variables critical section (block), also known as synchronized block
}
Format 2: synchronized method. In the method preceded by synchronized, such as:
public synchronized void add () {
/ / Critical section
}
Shared variables associated with the object lock is how to choose? Namely
synchronized (any object) {
/ / Critical section
}
1.synchronized lock is a specific object, usually a shared variable objects. Enclosed with a synchronized block access to the shared variable is critical areas, namely synchronized block of code. Since all threads lock the same object, between the synchronized code block are mutually exclusive, that is, these threads between the synchronized code block is executed serially, and are no longer mutually alternately interspersed with concurrent execution, thus ensuring the synchronized code block atomicity of operations. However, the synchronized block to all threads between non-synchronized code block, and non-synchronized and non-synchronized code block of code blocks are executed concurrently with each other alternately interspersed, the synchronized code block atomicity of operations is a logical, rather than physically non-interrupted.
(2) Every Java object has one and only one object lock. At any time, the lock of an object owned by only one thread. If two or more threads lock is not the same object, they are synchronized with each other alternately interspersed with blocks of code execute concurrently.
3 All of the non-synchronized code block or method calls are free. If thread A won the object lock, call requires the object lock synchronized code block, other threads can still be free to call all non-synchronized methods and code
(4) If thread A won the object O object lock, call the object O synchronized code block or method, thread A can still call any other desired object O lock synchronized code block or method, it is because thread A has received the object O object lock. A thread can call both need another object of K lock synchronized code block or method, which means that objects have both thread A K O objects and object locks.
5 Only when a thread is executing it calls synchronized code block or method, whether it is properly executed, or an exception is thrown, the thread will release the acquired object lock. synchronized does not necessarily protect data. Programmers should be carefully analyzed to identify all of the critical process, and applying these critical sections synchronized mechanism. If one or multiple missing, the shared variable data will generate an error
6 The critical area of ??the shared variable should be defined as private type. Otherwise, other methods of the class may be directly access and manipulate the shared variable, so synchronized protection would be meaningless. So only through critical access to shared variables. Therefore locked object is usually this, namely usual format are: synchronized (this) {...}
7 must ensure that all access to shared variables and operations are carried out in the synchronized code block.
8 usually shared variables are instance variables. If the critical region of the shared variable is a class variable, the problem is complicated because the class methods and instance methods can access class variables. The lock must be synchronized object, not a class. If the proposal is a critical area of ??the shared variable is a class variable, you should use the class method to access operations such variables. This class method becomes a critical section, it must be defined as a class method synchronized method. To access all the shared class variable instance method should call the class is defined as the synchronized method. If the instance method must succeed in their own internal code, not through synchronized class method to access the shared class variables can be passed synchronized (class name. Class) {... to access the class lock. Java, each class has a class object, the class object is actually an instance of java.lang.Class object, the so-called class lock is a lock object of this class. Note Locks and instance objects of this class are the object lock object lock though, but it is a different two locks. All like synchronized (class name. Class ()) {= synchronized code block} This lock class object (note: not lock a certain class instance object), in which the synchronized code block, all serial execution, access or use Locks should be carefully considered and weighed
9 When a thread enters the dead state, all objects owned by the thread locks are released.
3.Lock object lock
Java SE5 introduced java.util.concurrent.lock library, which is to solve the problem of the second mechanism mutually exclusive. Lock with ReentrantLock class creates an object to protect critical areas. ReentrantLock protection code blocks with the basic structure is as follows.
private Lock locker = new ReentrantLock ();
locker.lock (); / / lock
try {
...
} Finally {
locker.unlock (); / / Unlock
}
lock () and unlock () must be matched to use. Must ensure that the lock () corresponding to the unlock () will be implemented. So you have to unlock () in the finally block to ensure that both the normal execution, or an exception is thrown, unlock () will be implemented.
synchronized and lock the difference between:
Lock locking is achieved through the code, which are synchronized to achieve the level of the JVM
synchronized method block if the lock when an exception is thrown, JVM will automatically release the lock, will not release the lock out of the exception is not caused by the thread deadlock. However, Lock, then you do not enjoy the JVM brings automatic function abnormal must finally releasing the lock off, otherwise it will lead to deadlock.
Resource competition is not very intense in the case of the occasional case of synchronized, synchronized is very appropriate. The reason is that the compiler will usually possible to optimize synchronize, another very good readability, regardless of use threads package never used more than 5.0 programmers can understand.

ReentrantLock:
ReentrantLock provides a variety of synchronization, such as a time-limited synchronization can be Interrupt synchronization (synchronized synchronization is not Interrupt a) and so on. In the fierce competition for resources is not the case, the performance is little more than a synchronized handicap points. But when synchronizing very intense time, synchronized performance can suddenly drop several times. And indeed ReentrantLock can maintain the norm.

Atomic:
similar to the above, the case is not intense, slightly worse performance than synchronized, and intense, but also to maintain the norm. Intense time, Atomic's performance will be better than ReentrantLock about double. But it has a drawback that can only sync one value, a code can only appear one Atomic variables, more than a synchronized void. Because he can not be synchronized between multiple Atomic.

没有评论:

发表评论