public RocksDBStore(String name, ColumnFamilyDescriptor family, ColumnFamilyHandle handle, RocksDB db, int stripes) { super(); this.family = family; this.name = name; this.db = db; this.parallel = stripes; this.handle = handle; this.sharedLocks = new AutoCloseableLock[stripes]; this.exclusiveLocks = new AutoCloseableLock[stripes]; for (int i = 0; i < stripes; i++) { ReadWriteLock core = new ReentrantReadWriteLock(); sharedLocks[i] = new AutoCloseableLock(core.readLock()); exclusiveLocks[i] = new AutoCloseableLock(core.writeLock()); } }
public App(AMContext context) { super(App.class.getName()); this.context = context; stateMachine = stateMachineFactory.make(this); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); this.readLock = readWriteLock.readLock(); this.writeLock = readWriteLock.writeLock(); this.launchTime = context.getStartTime(); shouldRetry = false; diagnostics = new ArrayList<String>(); stateTimeOutMs = context.getConf().getLong(AngelConf.ANGEL_AM_APPSTATE_TIMEOUT_MS, AngelConf.DEFAULT_ANGEL_AM_APPSTATE_TIMEOUT_MS); stateToTsMap = new HashMap<AppState, Long>(); stateToTsMap.put(AppState.NEW, context.getClock().getTime()); stopped = new AtomicBoolean(false); }
public AMPSAgent(AMContext context, PSAgentId id, Location location) { this.context = context; this.id = id; this.location = location; this.diagnostics = new ArrayList<String>(); this.failedAttempts = new ArrayList<PSAgentAttemptId>(); this.attempts = new HashMap<PSAgentAttemptId, PSAgentAttempt>(); if (context.getRunningMode() == RunningMode.ANGEL_PS_PSAGENT) { this.stateMachine = stateMachineFactoryForAllMode.make(this); } else { this.stateMachine = stateMachineFactoryForPSMode.make(this); } ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); this.readLock = readWriteLock.readLock(); this.writeLock = readWriteLock.writeLock(); }
public AMTask(TaskId id, AMTask amTask) { state = AMTaskState.NEW; taskId = id; metrics = new HashMap<String, String>(); startTime = -1; finishTime = -1; matrixIdToClockMap = new Int2IntOpenHashMap(); // if amTask is not null, we should clone task state from it if (amTask == null) { iteration = 0; progress = 0.0f; } else { iteration = amTask.getIteration(); progress = amTask.getProgress(); matrixIdToClockMap.putAll(amTask.matrixIdToClockMap); } ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); }
public AMWorker(WorkerId id, AMContext context, List<TaskId> taskIds) { this.id = id; this.context = context; this.taskIds = taskIds; ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); stateMachine = stateMachineFactory.make(this); metrics = new HashMap<String, String>(); diagnostics = new ArrayList<String>(); attempts = new HashMap<WorkerAttemptId, WorkerAttempt>(); failedAttempts = new HashSet<WorkerAttemptId>(); maxAttempts = context.getConf().getInt(AngelConf.ANGEL_WORKER_MAX_ATTEMPTS, AngelConf.DEFAULT_WORKER_MAX_ATTEMPTS); }
/** * 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>(); }
public static void main(String[] args) { //创建并发访问的账户 MyCount myCount = new MyCount("95599200901215522", 10000); //创建一个锁对象 ReadWriteLock lock = new ReentrantReadWriteLock(false); //创建一个线程池 ExecutorService pool = Executors.newFixedThreadPool(2); //创建一些并发访问用户,一个信用卡,存的存,取的取,好热闹啊 User u1 = new User("张三", myCount, -4000, lock, true); User u2 = new User("张三他爹", myCount, 6000, lock, false); User u3 = new User("张三他弟", myCount, -8000, lock, false); User u4 = new User("张三", myCount, 800, lock, false); User u5 = new User("张三他爹", myCount, 0, lock, true); //在线程池中执行各个用户的操作 pool.execute(u1); pool.execute(u2); pool.execute(u3); pool.execute(u4); pool.execute(u5); //关闭线程池 pool.shutdown(); }
public Context<ReadWriteLock> decorateDestructionCallback(final Runnable callback) { if (callback == null) { return null; } final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); return new Context<ReadWriteLock>(new Runnable() { public void run() { Lock lock = readWriteLock.writeLock(); lock.lock(); try { callback.run(); } finally { lock.unlock(); } } }, readWriteLock); }
private static List<Striped<?>> weakImplementations() { return ImmutableList.<Striped<?>>builder() .add(new Striped.SmallLazyStriped<ReadWriteLock>(50, READ_WRITE_LOCK_SUPPLIER)) .add(new Striped.SmallLazyStriped<ReadWriteLock>(64, READ_WRITE_LOCK_SUPPLIER)) .add(new Striped.LargeLazyStriped<ReadWriteLock>(50, READ_WRITE_LOCK_SUPPLIER)) .add(new Striped.LargeLazyStriped<ReadWriteLock>(64, READ_WRITE_LOCK_SUPPLIER)) .add(new Striped.SmallLazyStriped<Lock>(50, LOCK_SUPPLER)) .add(new Striped.SmallLazyStriped<Lock>(64, LOCK_SUPPLER)) .add(new Striped.LargeLazyStriped<Lock>(50, LOCK_SUPPLER)) .add(new Striped.LargeLazyStriped<Lock>(64, LOCK_SUPPLER)) .add(new Striped.SmallLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER)) .add(new Striped.SmallLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER)) .add(new Striped.LargeLazyStriped<Semaphore>(50, SEMAPHORE_SUPPLER)) .add(new Striped.LargeLazyStriped<Semaphore>(64, SEMAPHORE_SUPPLER)) .build(); }
protected StoreImage loadFromDisk(Path imgFile, long id) { byte[] imgData = null; // Try aquiring a lock for a file. ReadWriteLock l = getIDLock(id); l.readLock().lock(); try { imgData = Files.readAllBytes(imgFile); } catch (IOException ioException) { log.warn("An IOException occured while trying to read the file \"" + imgFile.toAbsolutePath() + "\" from disk. Returning null.", ioException); } finally { l.readLock().unlock(); } if (imgData == null) { return null; } ImageSize size = imgDB.getImageSize(id); if (size == null) { return null; } return new StoreImage(id, imgData, size); }
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; }
public ContainerImpl(Configuration conf, Dispatcher dispatcher, NMStateStoreService stateStore, ContainerLaunchContext launchContext, Credentials creds, NodeManagerMetrics metrics, ContainerTokenIdentifier containerTokenIdentifier) { this.daemonConf = conf; this.dispatcher = dispatcher; this.stateStore = stateStore; this.launchContext = launchContext; this.containerTokenIdentifier = containerTokenIdentifier; this.containerId = containerTokenIdentifier.getContainerID(); this.resource = containerTokenIdentifier.getResource(); this.diagnostics = new StringBuilder(); this.credentials = creds; this.metrics = metrics; user = containerTokenIdentifier.getApplicationSubmitter(); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); this.readLock = readWriteLock.readLock(); this.writeLock = readWriteLock.writeLock(); stateMachine = stateMachineFactory.make(this); }
public GenericDynamoDB(AmazonDynamoDB client, AWSCredentialsProvider awsCredentials, ClientConfiguration clientConfiguration, SecretsGroupIdentifier groupIdentifier, Class<Entry> clazz, Converters converters, ReadWriteLock readWriteLock) { this.clazz = clazz; buildMappings(); this.converters = converters; this.awsCredentials = awsCredentials; this.clientConfiguration = clientConfiguration; this.client = client; this.region = RegionUtils.getRegion(groupIdentifier.region.getName()); this.readWriteLock = readWriteLock; RegionLocalResourceName resourceName = new RegionLocalResourceName(groupIdentifier); this.tableName = resourceName.toString(); }
public GenericFile(java.io.File path, Converters converters, Encryptor encryptor, EncryptionContext encryptionContext, Class<Entry> clazz, ReadWriteLock readWriteLock) { this.file = path; this.converters = converters; this.clazz = clazz; this.readWriteLock = readWriteLock; this.encryptor = encryptor; this.encryptionContext = encryptionContext; buildMappings(); open(); }
public void backup(SecretsGroupIdentifier group, Store backupStore, boolean failIfBackupStoreAlreadyExists) { ReadWriteLock readWriteLock = getReadWriteLock(group); readWriteLock.writeLock().lock(); try { Store currentStore = getCurrentStore(group, readWriteLock); if (backupStore.exists()) { if (failIfBackupStoreAlreadyExists) { throw new AlreadyExistsException("The store to backup to already exists"); } backupStore.delete(); } backupStore.create(); currentStore.stream().forEach(backupStore::create); backupStore.close(); } finally { readWriteLock.writeLock().unlock(); } }
public void restore(SecretsGroupIdentifier group, Store backupStore, boolean failIfStoreToRestoreAlreadyExists) { ReadWriteLock readWriteLock = getReadWriteLock(group); readWriteLock.writeLock().lock(); try { Store currentStore = getCurrentStore(group, readWriteLock); if (currentStore.exists()) { if (failIfStoreToRestoreAlreadyExists) { throw new AlreadyExistsException("The store to restore already exists"); } currentStore.delete(); } currentStore.create(); backupStore.stream().forEach(currentStore::create); currentStore.close(); } finally { readWriteLock.writeLock().unlock(); } }
@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; }
public AMParameterServer(String ip, ParameterServerId id, AMContext context) { this.ip = ip; this.id = id; this.context = context; ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); stateMachine = stateMachineFactory.make(this); attempts = new HashMap<PSAttemptId, PSAttempt>(2); this.failedAttempts = new HashSet<PSAttemptId>(2); maxAttempts = context.getConf().getInt(AngelConf.ANGEL_PS_MAX_ATTEMPTS, AngelConf.DEFAULT_PS_MAX_ATTEMPTS); }
PendingRequests(final Collection<String> groups, final ReadWriteLock rwLock) { Map<String, Set<String>> requestSourceToRequestID = new HashMap<>(); Map<String, Lock> groupLocks = new HashMap<>(); for (String group : groups) { groupLocks.put(group, new ReentrantLock()); requestSourceToRequestID.put(group, new HashSet<>()); } this.requestSourceToRequestID = Collections.unmodifiableMap(requestSourceToRequestID); this.groupLocks = Collections.unmodifiableMap(groupLocks); this.rwLock = rwLock; }
public YarnContainerAllocator(AMContext context) { super(YarnContainerAllocator.class.getName()); this.context = context; this.recordFactory = RecordFactoryProvider.getRecordFactory(null); stopped = new AtomicBoolean(false); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); }
public AMMatrixMetaManager(AMContext context) { this.context = context; matrixMetaManager = new MatrixMetaManager(); matrixPartitionsOnPS = new HashMap<>(); matrixIdToPSSetMap = new HashMap<>(); psIdToMatrixIdsMap = new HashMap<>(); psIdToRecoverPartsMap = new ConcurrentHashMap<>(); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); }
public PSAgentAttempt(PSAgentAttemptId attemptId, AMContext context, Location location) { this.id = attemptId; this.context = context; this.location = location; if (context.getRunningMode() == RunningMode.ANGEL_PS_PSAGENT) { stateMachine = stateMachineFactoryForAllMode.make(this); } else { stateMachine = stateMachineFactoryForPSMode.make(this); } ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLock = readWriteLock.readLock(); writeLock = readWriteLock.writeLock(); }
/** * Instantiates a new Worker. * * @param conf the conf * @param appId the app id * @param user the user * @param workerAttemptId the worker attempt id * @param masterLocation the master location * @param initMinClock the init min clock * @param isLeader the is leader */ public Worker(Configuration conf, ApplicationId appId, String user, WorkerAttemptId workerAttemptId, Location masterLocation, int initMinClock, boolean isLeader) { this.conf = conf; this.stopped = new AtomicBoolean(false); this.workerInitFinishedFlag = new AtomicBoolean(false); this.exitedFlag = new AtomicBoolean(false); this.workerMetrics = new HashMap<String, String>(); this.connection = TConnectionManager.getConnection(conf); ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); readLockForTaskNum = readWriteLock.readLock(); writeLockForTaskNum = readWriteLock.writeLock(); activeTaskNum = conf.getInt(AngelConf.ANGEL_TASK_ACTUAL_NUM, 1); this.appId = appId; this.user = user; this.masterLocation = masterLocation; this.workerAttemptId = workerAttemptId; this.isLeader = isLeader; this.workerAttemptIdProto = ProtobufUtil.convertToIdProto(workerAttemptId); this.initMinClock = initMinClock; this.counterUpdater = new CounterUpdater(); WorkerContext.get().setWorker(this); }
<T> T readAction( @NonNull final FileObject file, @NonNull final Callable<T> action) throws Exception { Parameters.notNull("file", file); //NOI18N Parameters.notNull("action", action); //NOI18N final ReadWriteLock lck = getLock(file); lck.readLock().lock(); try { return action.call(); } finally { lck.readLock().unlock(); } }
<T> T writeAction( @NonNull final FileObject file, @NonNull final Callable<T> action) throws Exception { Parameters.notNull("file", file); //NOI18N Parameters.notNull("action", action); //NOI18N final ReadWriteLock lck = getLock(file); lck.writeLock().lock(); try { return action.call(); } finally { lck.writeLock().unlock(); } }
/** * The constructor takes a {@link KeyProviderCryptoExtension} and an * implementation of <code>KeyACLs</code>. All calls are delegated to the * provider keyProvider after authorization check (if required) * @param keyProvider the key provider * @param acls the Key ACLs */ public KeyAuthorizationKeyProvider(KeyProviderProxyReEncryptionExtension keyProvider, KeyACLs acls) { super(keyProvider, null); this.provider = keyProvider; this.acls = acls; ReadWriteLock lock = new ReentrantReadWriteLock(true); readLock = lock.readLock(); writeLock = lock.writeLock(); }
User(String name, MyCount myCount, int iocash, ReadWriteLock myLock, boolean ischeck) { this.name = name; this.myCount = myCount; this.iocash = iocash; this.myLock = myLock; this.ischeck = ischeck; }
private void readWriteLock() { ExecutorService executor = Executors.newFixedThreadPool(2); Map<String, String> map = new HashMap<>(); ReadWriteLock lock = new ReentrantReadWriteLock(); executor.submit(() -> { lock.writeLock().lock(); try { sleep(1); map.put("foo", "bar"); } finally { lock.writeLock().unlock(); } }); Runnable readTask = () -> { lock.readLock().lock(); try { System.out.println(map.get("foo")); sleep(1); } finally { lock.readLock().unlock(); } }; executor.submit(readTask); executor.submit(readTask); stop(executor); }
@Test public void testReadLockReadUnlock() { final ReadWriteLock rwl = new CountingReadWriteLock(); final Lock r = rwl.readLock(); r.lock(); r.unlock(); }
@Test public void testWriteLockWriteUnlock() { final ReadWriteLock rwl = new CountingReadWriteLock(); final Lock w = rwl.writeLock(); w.lock(); w.unlock(); }
@Test public void testWriteLockBlocksReadLock() { final ReadWriteLock rwl = new CountingReadWriteLock(); final Lock w = rwl.writeLock(); w.lock(); final Lock r = rwl.readLock(); assertFalse(r.tryLock()); }
@Test public void testWriteLockBlocksWriteLock() { final ReadWriteLock rwl = new CountingReadWriteLock(); final Lock w = rwl.writeLock(); w.lock(); assertFalse(w.tryLock()); }
@Test public void testReadLockBlocksWriteLock() { final ReadWriteLock rwl = new CountingReadWriteLock(); final Lock r = rwl.readLock(); r.lock(); final Lock w = rwl.writeLock(); assertFalse(w.tryLock()); }
@Test public void testReadLockDoesNotBlockReadLock() { final ReadWriteLock rwl = new CountingReadWriteLock(); final Lock r = rwl.readLock(); r.lock(); assertTrue(r.tryLock()); }
public MetadataExtracterRegistry() { // initialise lists extracters = new ArrayList<MetadataExtracter>(10); extracterCache = new HashMap<String, List<MetadataExtracter>>(17); embedderCache = new HashMap<String, List<MetadataEmbedder>>(17); // create lock objects for access to the cache ReadWriteLock extractionCacheLock = new ReentrantReadWriteLock(); extracterCacheReadLock = extractionCacheLock.readLock(); extracterCacheWriteLock = extractionCacheLock.writeLock(); }
private ReadWriteLock getLockInProcessQueue(ProcessQueue pq) { try { return (ReadWriteLock) FieldUtils.readDeclaredField(pq, "lockTreeMap", true); } catch (IllegalAccessException e) { return null; } }
/** * Constructs a new, empty SubjectMap */ public SubjectMap() { ReadWriteLock _readWriteLock = new ReentrantReadWriteLock(); _readLock = _readWriteLock.readLock(); _writeLock = _readWriteLock.writeLock(); _weakCache = new HashMap<>(); _cache = new HashMap<>(); _faults = BehaviorProcessor.create(); _weakSources = new HashMap<>(); }
public ConfigurationFactoryImpl () { final ReadWriteLock lock = new ReentrantReadWriteLock (); this.readLock = lock.readLock (); this.writeLock = lock.writeLock (); final BundleContext context = FrameworkUtil.getBundle ( DataContext.class ).getBundleContext (); this.executor = new ScheduledExportedExecutorService ( "org.eclipse.scada.da.server.exporter.rest", 1 ); this.hiveSource = new ServiceListenerHiveSource ( context, this.executor ); this.hiveSource.open (); }
public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(2); Map<String, String> map = new HashMap<>(); ReadWriteLock lock = new ReentrantReadWriteLock(); executor.submit(() -> { lock.writeLock().lock(); try { ConcurrentUtils.sleep(1); map.put("foo", "bar"); } finally { lock.writeLock().unlock(); } }); Runnable readTask = () -> { lock.readLock().lock(); try { System.out.println(map.get("foo")); ConcurrentUtils.sleep(1); } finally { lock.readLock().unlock(); } }; executor.submit(readTask); executor.submit(readTask); ConcurrentUtils.stop(executor); }