下面列出了java.util.concurrent.locks.ReentrantReadWriteLock#writeLock() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
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);
}
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();
}
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 ();
}
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();
// }
// });
}
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 ();
}
/**
* Creates a new instance of tag set.
*/
public TagTypeSet() {
tags = new ConcurrentHashMap<>();
final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
read = rwLock.readLock();
write = rwLock.writeLock();
}
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);
}
public RMAppAttemptMetrics(ApplicationAttemptId attemptId,
RMContext rmContext) {
this.attemptId = attemptId;
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
this.readLock = lock.readLock();
this.writeLock = lock.writeLock();
this.rmContext = rmContext;
}
public MoveCapableCommonRoutingContentStore()
{
super();
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
this.storesCacheReadLock = lock.readLock();
this.storesCacheWriteLock = lock.writeLock();
}
/**
* Defaults
*/
public RepoUsageComponentImpl()
{
this.restrictions = new RepoUsage(null, null, null, LicenseMode.UNKNOWN, null, false);
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
restrictionsReadLock = lock.readLock();
restrictionsWriteLock = lock.writeLock();
}
public ProjectLockImpl() {
projectLocks = new HashMap<String, Lock>();
rwlock = new ReentrantReadWriteLock();
rlock = rwlock.readLock();
wlock = rwlock.writeLock();
waiting = false;
}
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();
}
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);
}
@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();
}
}
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();
}
public FileCacheHelper(String path) {
mPath = path;
mReentrantLock = new ReentrantReadWriteLock();
mReadLock = mReentrantLock.readLock();
mWriteLock = mReentrantLock.writeLock();
}
protected AbstractRoutingContentStore()
{
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
storesCacheReadLock = lock.readLock();
storesCacheWriteLock = lock.writeLock();
}
public FeatureExtractor() {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
r = lock.readLock();
w = lock.writeLock();
}
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);
}