当前位置: 首页>>代码示例>>Java>>正文


Java ReentrantReadWriteLock类代码示例

本文整理汇总了Java中java.util.concurrent.locks.ReentrantReadWriteLock的典型用法代码示例。如果您正苦于以下问题:Java ReentrantReadWriteLock类的具体用法?Java ReentrantReadWriteLock怎么用?Java ReentrantReadWriteLock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ReentrantReadWriteLock类属于java.util.concurrent.locks包,在下文中一共展示了ReentrantReadWriteLock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: get

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
final V get(final Object key, final int hash, final MapCallback<K, V, ?, ?> readCallback) {
  final ReentrantReadWriteLock.ReadLock readLock = super.readLock();
  readLock.lock();
  try {
    if (this.count != 0) { // read-volatile
      HashEntry<K, V> e = getFirst(hash);
      while (e != null) {
        if (e.getEntryHash() == hash && equalityKeyCompare(key, e)) {
          final V v = e.getMapValue();
          if (v != null) {
            if (readCallback != null) {
              readCallback.oldValueRead(v);
            }
            return v;
          }
        }
        e = e.getNextEntry();
      }
    }
  } finally {
    readLock.unlock();
  }
  return null;
}
 
开发者ID:ampool,项目名称:monarch,代码行数:25,代码来源:CustomEntryConcurrentHashMap.java

示例2: cloneRow

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
private TVector cloneRow(int matrixId, int rowIndex, TVector row, TaskContext taskContext) {
  if (row == null) {
    return null;
  }

  if (isNeedClone(matrixId)) {
    ReentrantReadWriteLock globalStorage =
        PSAgentContext.get().getMatrixStorageManager().getMatrixStoage(matrixId).getLock();
    TVector taskRow = taskContext.getMatrixStorage().getRow(matrixId, rowIndex);
    try {
      globalStorage.readLock().lock();
      if(taskRow == null || (taskRow.getClass() != row.getClass())){
        taskRow = row.clone();
        taskContext.getMatrixStorage().addRow(matrixId, rowIndex, taskRow);
      } else {
        taskRow.clone(row);
      }
    } finally {
      globalStorage.readLock().unlock();
    }
    return taskRow;
  } else {
    return row;
  }
}
 
开发者ID:Tencent,项目名称:angel,代码行数:26,代码来源:ConsistencyController.java

示例3: writeObject

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
/**
 * Save the state of the <tt>ConcurrentHashMap</tt> instance to a stream (i.e., serialize it).
 * 
 * @param s the stream
 * @serialData the key (Object) and value (Object) for each key-value mapping, followed by a null
 *             pair. The key-value mappings are emitted in no particular order.
 */
private void writeObject(final java.io.ObjectOutputStream s) throws IOException {
  s.defaultWriteObject();

  for (int k = 0; k < this.segments.length; ++k) {
    final Segment<K, V> seg = this.segments[k];
    final ReentrantReadWriteLock.ReadLock readLock = seg.readLock();
    readLock.lock();
    try {
      final HashEntry<K, V>[] tab = seg.table;
      for (int i = 0; i < tab.length; ++i) {
        for (HashEntry<K, V> e = tab[i]; e != null; e = e.getNextEntry()) {
          s.writeObject(e.getKey());
          s.writeObject(e.getMapValue());
        }
      }
    } finally {
      readLock.unlock();
    }
  }
  s.writeObject(null);
  s.writeObject(null);
}
 
开发者ID:ampool,项目名称:monarch,代码行数:30,代码来源:CustomEntryConcurrentHashMap.java

示例4: 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;
}
 
开发者ID:ponkin,项目名称:bloom,代码行数:25,代码来源:CuckooFilter.java

示例5: testMultipleReadLocks

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
public void testMultipleReadLocks(boolean fair) {
    final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(fair);
    lock.readLock().lock();
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            assertTrue(lock.readLock().tryLock());
            lock.readLock().unlock();
            assertTrue(lock.readLock().tryLock(LONG_DELAY_MS, MILLISECONDS));
            lock.readLock().unlock();
            lock.readLock().lock();
            lock.readLock().unlock();
        }});

    awaitTermination(t);
    lock.readLock().unlock();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:ReentrantReadWriteLockTest.java

示例6: AMWorkerGroup

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
/**
 * Create a AMWorkerGroup
 * @param groupId worker group id
 * @param context master context
 * @param workerMap workers contains in worker group
 * @param leader leader worker of worker group
 * @param splitIndex training data block index assgined to this worker group
 */
public AMWorkerGroup(WorkerGroupId groupId, AMContext context, Map<WorkerId, AMWorker> workerMap,
    WorkerId leader, int splitIndex) {
  this.context = context;
  this.groupId = groupId;
  this.workerMap = workerMap;
  this.leader = leader;
  this.splitIndex = splitIndex;

  ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
  readLock = readWriteLock.readLock();
  writeLock = readWriteLock.writeLock();
  stateMachine = stateMachineFactory.make(this);
  diagnostics = new ArrayList<String>();
  successWorkerSet = new HashSet<WorkerId>();
  failedWorkerSet = new HashSet<WorkerId>();
  killedWorkerSet = new HashSet<WorkerId>();
}
 
开发者ID:Tencent,项目名称:angel,代码行数:26,代码来源:AMWorkerGroup.java

示例7: replace

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
final V replace(final K key, final int hash, 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();
    }

    V oldValue = null;
    if (e != null) {
      oldValue = e.getMapValue();
      e.setMapValue(newValue);
    }
    return oldValue;
  } finally {
    writeLock.unlock();
  }
}
 
开发者ID:ampool,项目名称:monarch,代码行数:20,代码来源:CustomEntryConcurrentHashMap.java

示例8: load

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
@Override
public boolean load() {
    try {
        InstantFixClassMap.setClassLoader(getClass().getClassLoader());
        HashMap<Integer, ReadWriteLock> lockMap = new HashMap<>();
        HashMap<Integer, String> classIndexMap = new HashMap<>();
        String[] patchedClasses = getPatchedClasses();
        int[] patchedClassIndexes = getPatchedClassIndexes();
        if (patchedClasses.length != patchedClassIndexes.length) {
            throw new IllegalArgumentException("patchedClasses's len is " + patchedClasses.length + ", but patchedClassIndexs's len is " + patchedClassIndexes.length);
        }
        for (int i = 0; i < patchedClasses.length; i++) {
            String className = patchedClasses[i];
            int classIndex = patchedClassIndexes[i];
            lockMap.put(classIndex, new ReentrantReadWriteLock());
            classIndexMap.put(classIndex, className);
            Log.i(TAG, String.format("patched %s", className));
        }
        InstantFixClassMap.setAtomMap(new InstantFixClassMap.AtomMap(classIndexMap, lockMap));
    } catch (Throwable e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
 
开发者ID:meili,项目名称:Aceso,代码行数:26,代码来源:AbstractPatchesLoaderImpl.java

示例9: 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();
    }
  }
}
 
开发者ID:ponkin,项目名称:bloom,代码行数:17,代码来源:StableBloomFilter.java

示例10: YfyBaseClient

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
public YfyBaseClient(K key,
                     YfyRequestConfig requestConfig,
                     String accessToken,
                     String refreshToken,
                     YfyRefreshListener<K> refreshListener) {
    if (accessToken == null) {
        throw new NullPointerException("access token");
    }

    this.requestConfig = requestConfig;
    this.host = YfyAppInfo.getHost();
    this.refreshLock = new ReentrantReadWriteLock();

    this.key = key;
    this.accessToken = accessToken;
    this.refreshToken = refreshToken;
    if (refreshToken != null && refreshListener != null) {
        this.autoRefresh = true;
        this.refreshListener = refreshListener;
    }
}
 
开发者ID:yifangyun,项目名称:fangcloud-java-sdk,代码行数:22,代码来源:YfyBaseClient.java

示例11: StableBloomFilter

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
StableBloomFilter(BitSet bitset, long numOfBuckets, int bitsPerBucket, long bucketsToDecrement, int numHashFunctions, HashFunction strategy) {
  // allow 1 item per bucket
  this.bucketSet = new BucketSet(bitsPerBucket, 1, numOfBuckets, bitset);
  this.numHashFunctions = numHashFunctions;
  this.numOfBuckets = numOfBuckets;
  this.bitsPerBucket = bitsPerBucket;
  this.strategy = strategy;
  this.bucketsToDecrement = bucketsToDecrement;
  for(int i=0;i<DEFAULT_CONCURRENCY_LEVEL;i++) {
    segments[i] = new ReentrantReadWriteLock();
  }
  log.log(
      Level.FINE,
      String.format(
        "Stable Bloom filter: %1$d hash functions, %2$d bits, %3$d bits per elemnent",
        numHashFunctions,
        bitset.bitSize(),
        bitsPerBucket)
      );
}
 
开发者ID:ponkin,项目名称:bloom,代码行数:21,代码来源:StableBloomFilter.java

示例12: Translog

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
public Translog(TranslogConfig config, String nodeId) {
    super(config.getShardId(), config.getIndexSettings());
    this.config = null;
    recoveredTranslogs = null;
    syncScheduler = null;
    bigArrays = null;
    ReadWriteLock rwl = new ReentrantReadWriteLock();
    readLock = new ReleasableLock(rwl.readLock());
    writeLock = new ReleasableLock(rwl.writeLock());
    location = null;
    current = null;
    currentCommittingTranslog = null;
    lastCommittedTranslogFileGeneration = -1; 
    config = null;
    translogUUID = null;
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:17,代码来源:Translog.java

示例13: 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;
}
 
开发者ID:ponkin,项目名称:bloom,代码行数:20,代码来源:StableBloomFilter.java

示例14: HiveCommon

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
public HiveCommon ()
{
    final ReentrantReadWriteLock itemMapLock = new ReentrantReadWriteLock ( Boolean.getBoolean ( "org.eclipse.scada.da.server.common.fairItemMapLock" ) );

    this.itemMapReadLock = itemMapLock.readLock ();
    this.itemMapWriteLock = itemMapLock.writeLock ();

    this.subscriptionValidator = new SubscriptionValidator<String> () {

        @Override
        public boolean validate ( final SubscriptionListener<String> listener, final String topic )
        {
            return validateItem ( topic );
        }
    };
}
 
开发者ID:eclipse,项目名称:neoscada,代码行数:17,代码来源:HiveCommon.java

示例15: RMAppAttemptImpl

import java.util.concurrent.locks.ReentrantReadWriteLock; //导入依赖的package包/类
public RMAppAttemptImpl(ApplicationAttemptId appAttemptId,
    RMContext rmContext, YarnScheduler scheduler,
    ApplicationMasterService masterService,
    ApplicationSubmissionContext submissionContext,
    Configuration conf, boolean maybeLastAttempt, ResourceRequest amReq) {
  this.conf = conf;
  this.applicationAttemptId = appAttemptId;
  this.rmContext = rmContext;
  this.eventHandler = rmContext.getDispatcher().getEventHandler();
  this.submissionContext = submissionContext;
  this.scheduler = scheduler;
  this.masterService = masterService;

  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  this.readLock = lock.readLock();
  this.writeLock = lock.writeLock();

  this.proxiedTrackingUrl = generateProxyUriWithScheme();
  this.maybeLastAttempt = maybeLastAttempt;
  this.stateMachine = stateMachineFactory.make(this);

  this.attemptMetrics =
      new RMAppAttemptMetrics(applicationAttemptId, rmContext);
  
  this.amReq = amReq;
}
 
开发者ID:naver,项目名称:hadoop,代码行数:27,代码来源:RMAppAttemptImpl.java


注:本文中的java.util.concurrent.locks.ReentrantReadWriteLock类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。