- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 StampedLock
实现缓存读取或加载方法,我想知道是否可以改编 javadoc 中的示例比这更好。
作者 Doug Lea 给出了一个“乐观读”示例和一个“将读锁升级为写锁”示例,但在我的用例中,我想同时执行这两个示例。
public String getConverted(String input) {
String customised = null;
long stamp = 0L;
while (customised == null) {
if (!lock.validate(stamp)) {
stamp = lock.tryOptimisticRead();
}
customised = cached.get(input);
// if the lock was valid we can trust the value of customised
if (lock.validate(stamp) && customised == null) {
long writeStamp = 0L;
try {
while (customised == null) {
writeStamp = lock.tryConvertToWriteLock(stamp);
if (writeStamp != 0L) {
stamp = writeStamp;
customised = convertToCustom(input);
cached.put(input, customised);
} else {
// so what do we do here (line 15)?
}
}
} finally {
lock.unlock(stamp);
}
} else {
// if the lock was invalid, customised could be anything, so:
customised = null;
// and what do we do here (line 25)?
}
}
return customised;
}
因此,算法中有两点我需要做一些事情 - 在这两种情况下:
获取硬锁 - 第 15 行:
lock.unlockRead(stamp);
stamp = lock.writeLock();
第 25 行:
stamp = lock.readLock();
或者什么?
Thread.sleep(25);
这对我来说并不合适 - 当然 StampedLock
可以更好地管理该线程上的阻塞!
但是怎么办呢?如果我只是调用 readLock() 或 writeLock() ,那么我就放弃了 StampedLock#tryOptimisticRead() 内希望经过良好编码和测试的排队算法和StampedLock#tryConvertToWriteLock()。
或者这些方法背后的逻辑是否已经因为失败一次而被放弃?
最佳答案
您可以引用以下方法。
tryConvertToWriteLock 和 tryOptimisticRead 中没有排队机制。
Method tryOptimisticRead() returns a non-zero stamp only if the lock is not currently held in write mode. Method validate(long) returns true if the lock has not been acquired in write mode since obtaining a given stamp. This mode can be thought of as an extremely weak version of a read-lock, that can be broken by a writer at any time. The use of optimistic mode for short read-only code segments often reduces contention and improves throughput. However, its use is inherently fragile. Optimistic read sections should only read fields and hold them in local variables for later use after validation. Fields read while in optimistic mode may be wildly inconsistent, so usage applies only when you are familiar enough with data representations to check consistency and/or repeatedly invoke method validate(). For example, such steps are typically required when first reading an object or array reference, and then accessing one of its fields, elements or methods.
此外,我喜欢检查乐观锁定有效性的控制流,如果发现它无效,则获取锁定,因为它避免了代码中使用的“else” block 的需要。
tryConvertToWriteLock 的代码流程类似。
private final StampedLock sl = new StampedLock();
/**
* This method is to show the feature of tryOptimisticRead() method
*/
public double getTotalRevenueOptimisticRead() {
long stamp = sl.tryOptimisticRead();
double balance = this.totalRevenue;
boolean lockAcquired = false;
//calling validate(stamp) method to ensure that stamp is valid, if not then acquiring the read lock
if (!sl.validate(stamp)){
lockAcquired = true;
LOG.info("stamp for tryOptimisticRead() is not valid now, so acquiring the read lock");
stamp = sl.readLock();
}
try {
balance = this.totalRevenue;
} finally {
if(lockAcquired){
sl.unlockRead(stamp);
}
}
return balance;
}
/**
* This method is to show the feature of tryConvertToWriteLock() method
*/
public double getStateTaxReturnUisngConvertToWriteLock(TaxPayer taxPayer) {
double incomeTaxRetunAmount = taxPayer.getTaxAmount() * 5/100;
long stamp = sl.readLock();
boolean lockAcquired = false;
//Trying to upgrade the lock from read to write
stamp = sl.tryConvertToWriteLock(stamp);
//Checking if tryConvertToWriteLock got success otherwise call writeLock method
if(stamp == 0L){
LOG.info("stamp is zero for tryConvertToWriteLock(), so acquiring the write lock");
stamp = sl.writeLock();
lockAcquired = true;
}
try {
this.totalRevenue -= incomeTaxRetunAmount;
} finally {
if(lockAcquired){
sl.unlockWrite(stamp);
}
}
return incomeTaxRetunAmount;
}
关于java - StampedLock#tryOptimisticRead() 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50158850/
概览 在 JDK 1.8 引入 StampedLock,可以理解为对 ReentrantReadWriteLock 在某些方面的增强,在原先读写锁的基础上新增了一种叫乐观读(Opt
我正在调查基于 Java8 的 StampedLock ( javadoc here ) 锁定缓存,但我无法在网上找到令人信服的实现,尽管阅读了文章喜欢StampedLock Idioms . 在对
我们介绍了读写锁,学习完之后你应该已经知道“读写锁允许多个线程同时读共享变量,适用于读多写少的场景”。那在读多写少的场景中,还有没有更快的技术方案呢?还真有,Java 在 1.8 这个版本里,提供了一
我正在使用 StampedLock 实现缓存读取或加载方法,我想知道是否可以改编 javadoc 中的示例比这更好。 作者 Doug Lea 给出了一个“乐观读”示例和一个“将读锁升级为写锁”示例,但
http://winterbe.com/posts/2015/04/30/java8-concurrency-tutorial-synchronized-locks-examples/包含此代码: S
我一直在使用 ReadWriteLock `s 来实现/维护锁定习语。 自 JDK8 StampedLock已介绍。由于 RWLocks 以其缓慢和糟糕的性能而闻名,StampedLock 看起来像是
我正在编写以下代码。 CreateThread.java import java.util.concurrent.locks.*; class CreateThread { public s
给定来自 Oracle 文档的代码示例 https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/StampedLock
我正在编写 Java 代码,我需要在其中实现线程。我正在通过 JAVA 8 API 并开始了解 Stamped Locks。谁能告诉我为什么在多线程中使用 StampedLocks? 提前致谢。 最佳
一、写在开头 我们在上一篇写ReentrantReadWriteLock读写锁的末尾留了一个小坑,那就是读写锁因为写锁的悲观性,会导致 “写饥饿”,这样一来会大大的降低读写效率,而今天我们就来将
在 ReentrantLock 和 StampedLock 之间进行选择的用例应该是什么?例如,如果我有 10 个读者和 10 个写者,应该选择哪种锁?如果我有 20 名读者和 1 名作者,该选择哪一
我正面临关于 StampedLock 的奇怪行为.以下是主要有问题的代码行: StampedLock lock = new StampedLock(); long stamp1 = lock.read
从文档中还不清楚,如果在保持readLock的情况下尝试进行写入操作,那么读写器线程会发生什么情况。 我说的是这个时机: 阅读器线程(或某些阅读器线程)进入并获取readlock,目的是在某些多字段对
这comparison of StampedLock and other locks表明StampedLock随着竞争的加剧,是最快的。然而,这篇文章和其他各种文章都没有列出为什么它更快。它似乎使用与
最近我了解到存在StampedLock ? https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/locks/StampedL
我是一名优秀的程序员,十分优秀!