Java Code Examples for java.util.concurrent.locks.ReentrantReadWriteLock#writeLock()

The following examples show how to use java.util.concurrent.locks.ReentrantReadWriteLock#writeLock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From RxStore with Apache License 2.0 6 votes vote down vote up
static void runInWriteLock(ReentrantReadWriteLock readWriteLock, ThrowingRunnable runnable) {
  Lock readLock = readWriteLock.readLock();
  int readCount = readWriteLock.getWriteHoldCount() == 0 ? readWriteLock.getReadHoldCount() : 0;

  for (int i = 0; i < readCount; i++) {
    readLock.unlock();
  }

  Lock writeLock = readWriteLock.writeLock();
  writeLock.lock();

  try {
    runnable.run();
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    for (int i = 0; i < readCount; i++) {
      readLock.lock();
    }
    writeLock.unlock();
  }
}
 
Example 2
Source File: RMContainerImpl.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public RMContainerImpl(Container container,
    ApplicationAttemptId appAttemptId, NodeId nodeId,
    String user, RMContext rmContext, long creationTime) {
  this.stateMachine = stateMachineFactory.make(this);
  this.containerId = container.getId();
  this.nodeId = nodeId;
  this.container = container;
  this.appAttemptId = appAttemptId;
  this.user = user;
  this.creationTime = creationTime;
  this.rmContext = rmContext;
  this.eventHandler = rmContext.getDispatcher().getEventHandler();
  this.containerAllocationExpirer = rmContext.getContainerAllocationExpirer();
  this.isAMContainer = false;
  this.resourceRequests = null;

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

  rmContext.getRMApplicationHistoryWriter().containerStarted(this);
  rmContext.getSystemMetricsPublisher().containerCreated(
      this, this.creationTime);
}
 
Example 3
Source File: RowStoreAVLMemory.java    From evosql with Apache License 2.0 5 votes vote down vote up
public RowStoreAVLMemory(Table table) {

        this.database     = table.database;
        this.table        = table;
        this.indexList    = table.getIndexList();
        this.accessorList = new CachedObject[indexList.length];
        lock              = new ReentrantReadWriteLock();
        readLock          = lock.readLock();
        writeLock         = lock.writeLock();
    }
 
Example 4
Source File: ProjectLockImpl.java    From writelatex-git-bridge with MIT License 5 votes vote down vote up
public ProjectLockImpl() {
    projectLocks = new HashMap<String, Lock>();
    rwlock = new ReentrantReadWriteLock();
    rlock = rwlock.readLock();
    wlock = rwlock.writeLock();
    waiting = false;
}
 
Example 5
Source File: RepoUsageComponentImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Defaults
 */
public RepoUsageComponentImpl()
{
    this.restrictions = new RepoUsage(null, null, null, LicenseMode.UNKNOWN, null, false);
    
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    restrictionsReadLock = lock.readLock();
    restrictionsWriteLock = lock.writeLock();
}
 
Example 6
Source File: AbstractDataSourceHandler.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public AbstractDataSourceHandler ( final ObjectPoolTracker<DataSource> poolTracker )
{
    this.poolTracker = poolTracker;
    this.serviceListener = new ServiceListener () {

        @Override
        public void dataSourceChanged ( final DataSource dataSource )
        {
            AbstractDataSourceHandler.this.setDataSource ( dataSource );
        }
    };

    this.dataSourceListener = new DataSourceListener () {

        @Override
        public void stateChanged ( final DataItemValue value )
        {
            AbstractDataSourceHandler.this.stateChanged ( value );
        }
    };

    final ReentrantReadWriteLock lock = new ReentrantReadWriteLock ();
    this.dataSourceReadLock = lock.readLock ();
    this.dataSourceWriteLock = lock.writeLock ();

    this.trackerLock = new ReentrantLock ();
}
 
Example 7
Source File: HdfsKeyValueStore.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public HdfsKeyValueStore(boolean readOnly, Timer hdfsKeyValueTimer, Configuration configuration, Path path,
    long maxAmountAllowedPerFile, long maxTimeOpenForWriting) throws IOException {
  _readOnly = readOnly;
  _maxTimeOpenForWriting = maxTimeOpenForWriting;
  _maxAmountAllowedPerFile = maxAmountAllowedPerFile;
  _path = path;
  _fileSystem = _path.getFileSystem(configuration);
  _fileSystem.mkdirs(_path);
  _readWriteLock = new ReentrantReadWriteLock();
  _writeLock = _readWriteLock.writeLock();
  _readLock = _readWriteLock.readLock();
  _fileStatus.set(getSortedSet(_path));
  if (!_fileStatus.get().isEmpty()) {
    _currentFileCounter.set(Long.parseLong(_fileStatus.get().last().getPath().getName()));
  }
  removeAnyTruncatedFiles();
  loadIndexes();
  cleanupOldFiles();
  if (!_readOnly) {
    _idleLogTimerTask = getIdleLogTimer();
    _oldFileCleanerTimerTask = getOldFileCleanerTimer();
    _hdfsKeyValueTimer = hdfsKeyValueTimer;
    _hdfsKeyValueTimer.schedule(_idleLogTimerTask, DAEMON_POLL_TIME, DAEMON_POLL_TIME);
    _hdfsKeyValueTimer.schedule(_oldFileCleanerTimerTask, DAEMON_POLL_TIME, DAEMON_POLL_TIME);
  } else {
    _idleLogTimerTask = null;
    _oldFileCleanerTimerTask = null;
    _hdfsKeyValueTimer = null;
  }
  // Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, HDFS_KV, SIZE,
  // path.getParent().toString()), new Gauge<Long>() {
  // @Override
  // public Long value() {
  // return _size.get();
  // }
  // });
}
 
Example 8
Source File: EventInjectorImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public EventInjectorImpl ( final BundleContext context, final EventMonitorEvaluator evaluator )
{
    this.context = context;

    final ReentrantReadWriteLock rw = new ReentrantReadWriteLock ();
    this.readLock = rw.readLock ();
    this.writeLock = rw.writeLock ();

    this.evaluator = evaluator;

    this.executor = new ExportedExecutorService ( "org.eclipse.scada.ae.server.injector", 1, 1, 1, TimeUnit.MINUTES );
    this.factoryTracker = new EventHandlerFactoryTracker ( context, this.factoryListener );

    addDefault ();
}
 
Example 9
Source File: TagTypeSet.java    From JALSE with Apache License 2.0 5 votes vote down vote up
/**
    * Creates a new instance of tag set.
    */
   public TagTypeSet() {
tags = new ConcurrentHashMap<>();
final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
read = rwLock.readLock();
write = rwLock.writeLock();
   }
 
Example 10
Source File: ApplicationImpl.java    From big-c with Apache License 2.0 5 votes vote down vote up
public ApplicationImpl(Dispatcher dispatcher, String user, ApplicationId appId,
    Credentials credentials, Context context) {
  this.dispatcher = dispatcher;
  this.user = user;
  this.appId = appId;
  this.credentials = credentials;
  this.aclsManager = context.getApplicationACLsManager();
  this.context = context;
  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  readLock = lock.readLock();
  writeLock = lock.writeLock();
  stateMachine = stateMachineFactory.make(this);
}
 
Example 11
Source File: NMClientAsyncImpl.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public StatefulContainer(NMClientAsync client, ContainerId containerId) {
  this.nmClientAsync = client;
  this.containerId = containerId;
  stateMachine = stateMachineFactory.make(this);
  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  readLock = lock.readLock();
  writeLock = lock.writeLock();
}
 
Example 12
Source File: RMAppAttemptMetrics.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public RMAppAttemptMetrics(ApplicationAttemptId attemptId,
    RMContext rmContext) {
  this.attemptId = attemptId;
  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  this.readLock = lock.readLock();
  this.writeLock = lock.writeLock();
  this.rmContext = rmContext;
}
 
Example 13
Source File: MoveCapableCommonRoutingContentStore.java    From alfresco-simple-content-stores with Apache License 2.0 5 votes vote down vote up
public MoveCapableCommonRoutingContentStore()
{
    super();

    final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
    this.storesCacheReadLock = lock.readLock();
    this.storesCacheWriteLock = lock.writeLock();
}
 
Example 14
Source File: FileCacheHelper.java    From ShareBox with Apache License 2.0 4 votes vote down vote up
public FileCacheHelper(String path) {
    mPath = path;
    mReentrantLock = new ReentrantReadWriteLock();
    mReadLock = mReentrantLock.readLock();
    mWriteLock = mReentrantLock.writeLock();
}
 
Example 15
Source File: RMAppImpl.java    From big-c with Apache License 2.0 4 votes vote down vote up
public RMAppImpl(ApplicationId applicationId, RMContext rmContext,
    Configuration config, String name, String user, String queue,
    ApplicationSubmissionContext submissionContext, YarnScheduler scheduler,
    ApplicationMasterService masterService, long submitTime,
    String applicationType, Set<String> applicationTags, 
    ResourceRequest amReq) {

  this.systemClock = new SystemClock();

  this.applicationId = applicationId;
  this.name = name;
  this.rmContext = rmContext;
  this.dispatcher = rmContext.getDispatcher();
  this.handler = dispatcher.getEventHandler();
  this.conf = config;
  this.user = user;
  this.queue = queue;
  this.submissionContext = submissionContext;
  this.scheduler = scheduler;
  this.masterService = masterService;
  this.submitTime = submitTime;
  this.startTime = this.systemClock.getTime();
  this.applicationType = applicationType;
  this.applicationTags = applicationTags;
  this.amReq = amReq;

  int globalMaxAppAttempts = conf.getInt(YarnConfiguration.RM_AM_MAX_ATTEMPTS,
      YarnConfiguration.DEFAULT_RM_AM_MAX_ATTEMPTS);
  int individualMaxAppAttempts = submissionContext.getMaxAppAttempts();
  if (individualMaxAppAttempts <= 0 ||
      individualMaxAppAttempts > globalMaxAppAttempts) {
    this.maxAppAttempts = globalMaxAppAttempts;
    LOG.warn("The specific max attempts: " + individualMaxAppAttempts
        + " for application: " + applicationId.getId()
        + " is invalid, because it is out of the range [1, "
        + globalMaxAppAttempts + "]. Use the global max attempts instead.");
  } else {
    this.maxAppAttempts = individualMaxAppAttempts;
  }

  this.attemptFailuresValidityInterval =
      submissionContext.getAttemptFailuresValidityInterval();
  if (this.attemptFailuresValidityInterval > 0) {
    LOG.info("The attemptFailuresValidityInterval for the application: "
        + this.applicationId + " is " + this.attemptFailuresValidityInterval
        + ".");
  }

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

  this.stateMachine = stateMachineFactory.make(this);

  rmContext.getRMApplicationHistoryWriter().applicationStarted(this);
  rmContext.getSystemMetricsPublisher().appCreated(this, startTime);
}
 
Example 16
Source File: FeatureExtractor.java    From semafor-semantic-parser with GNU General Public License v3.0 4 votes vote down vote up
public FeatureExtractor() {
	ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	r = lock.readLock();
	w = lock.writeLock();
}
 
Example 17
Source File: AbstractRoutingContentStore.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected AbstractRoutingContentStore()
{
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    storesCacheReadLock = lock.readLock();
    storesCacheWriteLock = lock.writeLock();
}
 
Example 18
Source File: ContainerManagerImpl.java    From big-c with Apache License 2.0 4 votes vote down vote up
public ContainerManagerImpl(Context context, ContainerExecutor exec,
    DeletionService deletionContext, NodeStatusUpdater nodeStatusUpdater,
    NodeManagerMetrics metrics, ApplicationACLsManager aclsManager,
    LocalDirsHandlerService dirsHandler) {
  super(ContainerManagerImpl.class.getName());
  this.context = context;
  this.dirsHandler = dirsHandler;

  // ContainerManager level dispatcher.
  dispatcher = new AsyncDispatcher();
  this.deletionService = deletionContext;
  this.metrics = metrics;

  rsrcLocalizationSrvc =
      createResourceLocalizationService(exec, deletionContext, context);
  addService(rsrcLocalizationSrvc);

  containersLauncher = createContainersLauncher(context, exec);
  addService(containersLauncher);

  this.nodeStatusUpdater = nodeStatusUpdater;
  this.aclsManager = aclsManager;

  // Start configurable services
  auxiliaryServices = new AuxServices();
  auxiliaryServices.registerServiceListener(this);
  addService(auxiliaryServices);

  this.containersMonitor =
      new ContainersMonitorImpl(exec, dispatcher, this.context);
  addService(this.containersMonitor);

  dispatcher.register(ContainerEventType.class,
      new ContainerEventDispatcher());
  dispatcher.register(ApplicationEventType.class,
      new ApplicationEventDispatcher());
  dispatcher.register(LocalizationEventType.class, rsrcLocalizationSrvc);
  dispatcher.register(AuxServicesEventType.class, auxiliaryServices);
  dispatcher.register(ContainersMonitorEventType.class, containersMonitor);
  dispatcher.register(ContainersLauncherEventType.class, containersLauncher);
  
  addService(dispatcher);

  ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  this.readLock = lock.readLock();
  this.writeLock = lock.writeLock();
}
 
Example 19
Source File: CachingContentStore.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean delete(String contentUrl)
{
    if (contentUrl.startsWith(FileContentStore.SPOOF_PROTOCOL))
    {
        // This is not a failure but the content can never actually be deleted
        return false;
    }

    ReentrantReadWriteLock readWriteLock = readWriteLock(contentUrl);
    ReadLock readLock = readWriteLock.readLock();
    readLock.lock();
    try
    {
        if (!cache.contains(contentUrl))
        {
            // The item isn't in the cache, so simply delete from the backing store
            return backingStore.delete(contentUrl);
        }
    }
    finally
    {
        readLock.unlock();
    }
    
    WriteLock writeLock = readWriteLock.writeLock();
    writeLock.lock();
    try
    {
        // Double check the content still exists in the cache
        if (cache.contains(contentUrl))
        {
            // The item is in the cache, so remove.
            cache.remove(contentUrl);
            
        }
        // Whether the item was in the cache or not, it must still be deleted from the backing store.
        return backingStore.delete(contentUrl);
    }
    finally
    {
        writeLock.unlock();
    }
}
 
Example 20
Source File: RMAppImpl.java    From hadoop with Apache License 2.0 4 votes vote down vote up
public RMAppImpl(ApplicationId applicationId, RMContext rmContext,
    Configuration config, String name, String user, String queue,
    ApplicationSubmissionContext submissionContext, YarnScheduler scheduler,
    ApplicationMasterService masterService, long submitTime,
    String applicationType, Set<String> applicationTags, 
    ResourceRequest amReq) {

  this.systemClock = new SystemClock();

  this.applicationId = applicationId;
  this.name = name;
  this.rmContext = rmContext;
  this.dispatcher = rmContext.getDispatcher();
  this.handler = dispatcher.getEventHandler();
  this.conf = config;
  this.user = user;
  this.queue = queue;
  this.submissionContext = submissionContext;
  this.scheduler = scheduler;
  this.masterService = masterService;
  this.submitTime = submitTime;
  this.startTime = this.systemClock.getTime();
  this.applicationType = applicationType;
  this.applicationTags = applicationTags;
  this.amReq = amReq;

  int globalMaxAppAttempts = conf.getInt(YarnConfiguration.RM_AM_MAX_ATTEMPTS,
      YarnConfiguration.DEFAULT_RM_AM_MAX_ATTEMPTS);
  int individualMaxAppAttempts = submissionContext.getMaxAppAttempts();
  if (individualMaxAppAttempts <= 0 ||
      individualMaxAppAttempts > globalMaxAppAttempts) {
    this.maxAppAttempts = globalMaxAppAttempts;
    LOG.warn("The specific max attempts: " + individualMaxAppAttempts
        + " for application: " + applicationId.getId()
        + " is invalid, because it is out of the range [1, "
        + globalMaxAppAttempts + "]. Use the global max attempts instead.");
  } else {
    this.maxAppAttempts = individualMaxAppAttempts;
  }

  this.attemptFailuresValidityInterval =
      submissionContext.getAttemptFailuresValidityInterval();
  if (this.attemptFailuresValidityInterval > 0) {
    LOG.info("The attemptFailuresValidityInterval for the application: "
        + this.applicationId + " is " + this.attemptFailuresValidityInterval
        + ".");
  }

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

  this.stateMachine = stateMachineFactory.make(this);

  rmContext.getRMApplicationHistoryWriter().applicationStarted(this);
  rmContext.getSystemMetricsPublisher().appCreated(this, startTime);
}