本文整理汇总了Java中java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock方法的典型用法代码示例。如果您正苦于以下问题:Java ReentrantReadWriteLock.WriteLock方法的具体用法?Java ReentrantReadWriteLock.WriteLock怎么用?Java ReentrantReadWriteLock.WriteLock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.locks.ReentrantReadWriteLock
的用法示例。
在下文中一共展示了ReentrantReadWriteLock.WriteLock方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replace
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
final boolean replace(final K key, final int hash, final V oldValue, final V newValue) {
final ReentrantReadWriteLock.WriteLock writeLock = super.writeLock();
writeLock.lock();
try {
HashEntry<K, V> e = getFirst(hash);
while (e != null && (e.getEntryHash() != hash || !equalityKeyCompare(key, e))) {
e = e.getNextEntry();
}
boolean replaced = false;
if (e != null && equalityCompare(oldValue, e.getMapValue())) {
replaced = true;
e.setMapValue(newValue);
}
return replaced;
} finally {
writeLock.unlock();
}
}
示例2: put
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
@Override
public boolean put(byte[] bytes) {
long[] hashes = new long[numHashFunctions];
strategy.hashes(bytes, hashes);
// make room for new values
decrement();
for (int i = 0; i < hashes.length; i++) {
long idx = hashes[i] % numOfBuckets;
ReentrantReadWriteLock.WriteLock currentLock = segments[(int)(idx & FAST_MOD_32)].writeLock();
currentLock.lock();
try { // just in case something goes wrong
bucketSet.writeTag(idx, 0, Utils.MASKS[bitsPerBucket]); // write max val for bucket
} finally {
currentLock.unlock();
}
}
// forever true since we always overwrite bucket content
return true;
}
示例3: decrement
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
private void decrement() {
long pivot = ThreadLocalRandom.current().nextLong(numOfBuckets);
for(int i=0; i<bucketsToDecrement; i++) {
long idx = (pivot + i) % numOfBuckets;
ReentrantReadWriteLock.WriteLock currentLock = segments[(int)(idx & FAST_MOD_32)].writeLock();
currentLock.lock();
try { // just in case something goes wrong
long bucketVal = bucketSet.readTag(idx, 0);
if(bucketVal != 0L) {
bucketSet.writeTag(idx, 0, bucketVal-1);
}
} finally {
currentLock.unlock();
}
}
}
示例4: put
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
@Override
public boolean put(byte[] bytes) {
// each of k hashes has it`s own bit vector slice
long[] hashes = new long[numHashFunctions];
strategy.hashes(bytes, hashes);
boolean bitsChanged = false;
for (int i = 0; i < hashes.length; i++) {
long idx = i * sliceSize + (hashes[i] % sliceSize);
// x mod n = x & (n-1) if n equals power of 2
// idx is always positive
ReentrantReadWriteLock.WriteLock currentLock = segments[(int)(idx & FAST_MOD_32)].writeLock();
currentLock.lock();
try { // just in case something goes wrong
bitsChanged |= bits.set(idx);
} finally {
currentLock.unlock();
}
}
if(bitsChanged) {
numItems.incrementAndGet();
}
return bitsChanged;
}
示例5: clear
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
@Override
public void clear() {
ReentrantReadWriteLock.WriteLock[] locks =
new ReentrantReadWriteLock.WriteLock[segments.length];
for(int i=0; i<segments.length; i++) {
locks[i] = segments[i].writeLock();
locks[i].lock();
}
try { // just in case something goes wrong
bits.clear();
} finally {
for (ReentrantReadWriteLock.WriteLock seg : locks) {
seg.unlock();
}
}
}
示例6: put
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
@Override
public boolean put(byte[] item) {
long[] hashes = new long[2];
strategy.hashes(item, hashes);
long bucketIdx = hashes[0] % numBuckets;
long tag = fingerprint(hashes[1]);
boolean itemAdded = false;
ReentrantReadWriteLock.WriteLock lock = segments[(int)(bucketIdx & FAST_MOD_32)].writeLock();
lock.lock();
try {
itemAdded = table.append(bucketIdx, tag);
} finally {
lock.unlock();
}
if(!itemAdded) {
itemAdded = putInAlt(bucketIdx, tag);
}
if(itemAdded) {
count.incrementAndGet();
} else {
log.log(Level.WARNING, String.format("Cucko table exceed capacity: %1$d elements", count.get()));
}
return itemAdded;
}
示例7: putInAlt
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
/**
* Try to put tag in alternative bucket
*/
private boolean putInAlt(long bucketIdx, long tag) {
int kickNum = 0;
long altIdx = altIndex(bucketIdx, tag);
boolean itemAdded = false;
while(!itemAdded && kickNum < MAX_KICK_NUM ) {
ReentrantReadWriteLock.WriteLock lock = segments[(int)(altIdx & FAST_MOD_32)].writeLock();
lock.lock();
try {
itemAdded = table.append(altIdx, tag);
if(!itemAdded) {
int posToKick = ThreadLocalRandom.current().nextInt(tagsPerBucket);
long oldTag = table.readTag(altIdx, posToKick);
table.writeTag(altIdx, posToKick, tag);
tag = oldTag;
altIdx = altIndex(altIdx, oldTag);
}
} finally {
lock.unlock();
}
kickNum++;
}
return itemAdded;
}
示例8: put
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
final V put(final K key, final int hash, final V value, final boolean onlyIfAbsent) {
final ReentrantReadWriteLock.WriteLock writeLock = super.writeLock();
writeLock.lock();
try {
int c = this.count;
if (c++ > this.threshold) {
rehash();
}
final HashEntry<K, V>[] tab = this.table;
final int index = hash & (tab.length - 1);
final HashEntry<K, V> first = tab[index];
HashEntry<K, V> e = first;
while (e != null && (e.getEntryHash() != hash || !equalityKeyCompare(key, e))) {
e = e.getNextEntry();
}
final V oldValue;
if (e != null) {
oldValue = e.getMapValue();
if (!onlyIfAbsent) {
e.setMapValue(value);
}
} else {
oldValue = null;
++this.modCount;
tab[index] = this.entryCreator.newEntry(key, hash, first, value);
this.count = c; // write-volatile
}
return oldValue;
} finally {
writeLock.unlock();
}
}
示例9: releaseWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
/**
* Releases write lock, checking that it had a hold count of 1.
*/
void releaseWriteLock(PublicReentrantReadWriteLock lock) {
ReentrantReadWriteLock.WriteLock writeLock = lock.writeLock();
assertWriteLockedByMoi(lock);
assertEquals(1, lock.getWriteHoldCount());
writeLock.unlock();
assertNotWriteLocked(lock);
}
示例10: mergeInPlace
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
@Override
public Filter mergeInPlace(Filter other) throws Exception {
if (other == null) {
throw new IncompatibleMergeException("Cannot merge null stable bloom filter");
}
if (!(other instanceof StableBloomFilter)) {
throw new IncompatibleMergeException(
"Cannot merge bloom filter of class " + other.getClass().getName()
);
}
StableBloomFilter that = (StableBloomFilter) other;
if (this.bucketSet.sizeInBits() != that.bucketSet.sizeInBits()) {
throw new IncompatibleMergeException("Cannot merge bloom filters with different bit size");
}
if (this.numHashFunctions != that.numHashFunctions) {
throw new IncompatibleMergeException(
"Cannot merge bloom filters with different number of hash functions"
);
}
// lock all segments
ReentrantReadWriteLock.WriteLock[] locks =
new ReentrantReadWriteLock.WriteLock[segments.length];
for(int i=0; i<segments.length; i++) {
locks[i] = segments[i].writeLock();
locks[i].lock();
}
try {
this.bucketSet.putAll(that.bucketSet);
} finally {
for (ReentrantReadWriteLock.WriteLock seg : locks) {
seg.unlock();
}
}
return this;
}
示例11: mergeInPlace
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
@Override
public Filter mergeInPlace(Filter other) throws Exception {
if (other == null) {
throw new IncompatibleMergeException("Cannot merge null bloom filter");
}
if (!(other instanceof PartitionedBloomFilter)) {
throw new IncompatibleMergeException(
String.format("Cannot merge bloom filter of class %s", other.getClass().getName()));
}
PartitionedBloomFilter that = (PartitionedBloomFilter) other;
if (this.bitSize() != that.bitSize()) {
throw new IncompatibleMergeException("Cannot merge bloom filters with different bit size");
}
if (this.numHashFunctions != that.numHashFunctions) {
throw new IncompatibleMergeException(
"Cannot merge bloom filters with different number of hash functions");
}
// lock all segments
ReentrantReadWriteLock.WriteLock[] locks =
new ReentrantReadWriteLock.WriteLock[segments.length];
for(int i=0; i<segments.length; i++) {
locks[i] = segments[i].writeLock();
locks[i].lock();
}
try { // just in case something goes wrong
this.bits.putAll(that.bits);
} finally {
for (ReentrantReadWriteLock.WriteLock seg : locks) {
seg.unlock();
}
}
return this;
}
示例12: writeLock
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
public ReentrantReadWriteLock.WriteLock writeLock() {
return writeLock;
}
示例13: remove
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
/**
* Remove; match on key only if value null, else match both.
*/
// GemStone change
// added "condition" and "removeParams" parameters
final <C, P> V remove(final Object key, final int hash, final Object value,
final MapCallback<K, V, C, P> condition, final C context, final P removeParams) {
// End GemStone change
final ReentrantReadWriteLock.WriteLock writeLock = super.writeLock();
writeLock.lock();
try {
final int c = this.count - 1;
final HashEntry<K, V>[] tab = this.table;
final int index = hash & (tab.length - 1);
final HashEntry<K, V> first = tab[index];
HashEntry<K, V> e = first;
// GemStone change
// the entry previous to the matched one, if any
HashEntry<K, V> p = null;
while (e != null && (e.getEntryHash() != hash || !equalityKeyCompare(key, e))) {
e = e.getNextEntry();
if (p == null) {
p = first;
} else {
p = p.getNextEntry();
}
}
V oldValue = null;
if (e != null) {
final V v = e.getMapValue();
// GemStone change
// allow for passing in a null object for comparison during remove;
// also invoke the provided condition to check for removal
if ((value == NO_OBJECT_TOKEN || equalityCompareWithNulls(v, value))
&& (condition == null || condition.doRemoveValue(v, context, removeParams))) {
// End GemStone change
oldValue = v;
// All entries following removed node can stay in list,
// but all preceding ones need to be cloned.
++this.modCount;
// GemStone changes BEGIN
// update the next entry instead of cloning the nodes
// this is primarily because we don't want to change
// the underlying RegionEntry that may be used elsewhere
final ReentrantReadWriteLock.WriteLock listWriteLock = this.listUpdateLock.writeLock();
listWriteLock.lock();
try {
if (p == null) {
tab[index] = e.getNextEntry();
} else {
p.setNextEntry(e.getNextEntry());
}
} finally {
listWriteLock.unlock();
}
/*
* (original code) HashEntry<K, V> newFirst = e.next; for (HashEntry<K, V> p = first; p
* != e; p = p.next) { newFirst = this.entryCreator.newEntry(p.key, p.hash, newFirst,
* p.value); } tab[index] = newFirst;
*/
// GemStone changes END
this.count = c; // write-volatile
}
}
return oldValue;
} finally {
writeLock.unlock();
}
}
示例14: getWriteLock
import java.util.concurrent.locks.ReentrantReadWriteLock; //导入方法依赖的package包/类
/**
* @return the write lock for the server
*/
ReentrantReadWriteLock.WriteLock getWriteLock() {
return lock.writeLock();
}