public interface Lock源码探索

java.util.concurrent.locks
public interface Lock
Lock implementations provide more extensive locking operations than can be obtained using synchronized methods and statements. They allow more flexible structuring, may have quite different properties, and may support multiple associated Condition objects.
A lock is a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: only one thread at a time can acquire the lock and all access to the shared resource requires that the lock be acquired first. However, some locks may allow concurrent access to a shared resource, such as the read lock of a ReadWriteLock.
The use of synchronized methods or statements provides access to the implicit monitor lock associated with every object, but forces all lock acquisition and release to occur in a block-structured way##程序块的方式##: when multiple locks are acquired they must be released in the opposite order, and all locks must be released in the same lexical scope##词法范围## in which they were acquired.
While the scoping mechanism for synchronized methods and statements makes it much easier to program with monitor locks, and helps avoid many common programming errors involving locks, there are occasions where you need to work with locks in a more flexible way. For example, some algorithms for traversing concurrently accessed data structures require the use of "hand-over-hand"## 节节向上地## or "chain locking": you acquire the lock of node A, then node B, then release A and acquire C, then release B and acquire D and so on. Implementations of the Lock interface enable the use of such techniques by allowing a lock to be acquired and released in different scopes, and allowing multiple locks to be acquired and released in any order.
With this increased flexibility comes additional responsibility. The absence of block-structured locking removes the automatic release of locks that occurs with synchronized methods and statements. In most cases, the following idiom should be used:
     Lock l = ...;
       l.lock();
       try {
           // access the resource protected by this lock
       } finally {
           l.unlock();
       }
   
When locking and unlocking occur in different scopes, care must be taken to ensure that all code that is executed while the lock is held is protected by try-finally or try-catch to ensure that the lock is released when necessary.
Lock implementations provide additional functionality over the use of synchronized methods and statements by providing a non-blocking attempt to acquire a lock (tryLock()), an attempt to acquire the lock that can be interrupted (lockInterruptibly, and an attempt to acquire the lock that can timeout (tryLock(long, TimeUnit)).
A Lock class can also provide behavior and semantics that is quite different from that of the implicit monitor lock, such as guaranteed ordering, non-reentrant usage, or deadlock detection. If an implementation provides such specialized semantics then the implementation must document those semantics.
Note that Lock instances are just normal objects and can themselves be used as the target in a synchronized statement. Acquiring the monitor lock of a Lock instance has no specified relationship with invoking any of the lock methods of that instance. It is recommended that to avoid confusion you never use Lock instances in this way, except within their own implementation.

Except where noted, passing a null value for any parameter will result in a NullPointerException being thrown.


Memory Synchronization
All Lock implementations must enforce the same memory synchronization semantics as provided by the built-in monitor lock, as described in section 17.4 of The Java™ Language Specification:
A successful lock operation has the same memory synchronization effects as a successful Lock action.
A successful unlock operation has the same memory synchronization effects as a successful Unlock action.

Unsuccessful locking and unlocking operations, and reentrant locking/unlocking operations, do not require any memory synchronization effects.


Implementation Considerations
The three forms of lock acquisition (interruptible, non-interruptible, and timed) may differ in their performance characteristics, ordering guarantees, or other implementation qualities. Further, the ability to interrupt the ongoing acquisition of a lock may not be available in a given Lock class. Consequently##因此##, an implementation is not required to define exactly the same guarantees or semantics for all three forms of lock acquisition, nor is it required to support interruption of an ongoing lock acquisition. An implementation is required to clearly document the semantics and guarantees provided by each of the locking methods. It must also obey the interruption semantics as defined in this interface, to the extent##到...程度## that interruption of lock acquisition is supported: which is either totally, or only on method entry.
As interruption generally implies cancellation, and checks for interruption are often infrequent, an implementation can favor responding to an interrupt over normal method return##比普通方法返回,更喜欢responding to an interrupt##. This is true even if it can be shown that the interrupt occurred after another action may have unblocked the thread##Q整句##. An implementation should document this behavior.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,接口(interface)是一种特殊的抽象类,它只包含方法签名、常量和嵌套类型。可以将接口看作是一种协议,定义了一个类需要遵循的规范。 public interface关键字用于声明一个接口,其语法如下: ``` public interface 接口名称 { // 声明常量 public static final 数据类型 常量名称 = 值; // 声明方法 public 返回值类型 方法名称(参数列表); // 默认方法 public default 返回值类型 默认方法名称(参数列表) { // 方法体 } // 静态方法 public static 返回值类型 静态方法名称(参数列表) { // 方法体 } // 嵌套类型 public interface 嵌套类型名称 { // 声明常量、方法、嵌套类型等 } } ``` 接口中可以包含常量、抽象方法、默认方法、静态方法和嵌套类型等元素,其中常量和抽象方法是必须包含的。接口中的所有方法都是公共的,而且不能包含方法体。 实现接口的类必须实现接口中定义的所有抽象方法,否则需要将该类也声明为抽象类。类可以实现多个接口,使用逗号分隔即可。 例如: ``` public interface Shape { double getArea(); double getPerimeter(); } public class Circle implements Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public double getPerimeter() { return 2 * Math.PI * radius; } } ``` 在上面的例子中,Shape接口定义了getArea()和getPerimeter()两个抽象方法,Circle类实现了Shape接口,并且实现了接口中定义的两个方法。这样,我们就可以通过Circle类对象调用getArea()和getPerimeter()方法来计算圆的面积和周长了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值