Search in sources :

Example 31 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project hadoop by apache.

the class AbstractNodeLabelsProvider method serviceInit.

@Override
protected void serviceInit(Configuration conf) throws Exception {
    this.intervalTime = conf.getLong(YarnConfiguration.NM_NODE_LABELS_PROVIDER_FETCH_INTERVAL_MS, YarnConfiguration.DEFAULT_NM_NODE_LABELS_PROVIDER_FETCH_INTERVAL_MS);
    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    readLock = readWriteLock.readLock();
    writeLock = readWriteLock.writeLock();
    super.serviceInit(conf);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 32 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project atlas by alibaba.

the class BundleLock method WriteUnLock.

public static void WriteUnLock(String bundle) {
    ReentrantReadWriteLock lock = null;
    synchronized (bundleIdentifierMap) {
        lock = bundleIdentifierMap.get(bundle);
        if (lock == null) {
            return;
        }
    }
    lock.writeLock().unlock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 33 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project atlas by alibaba.

the class BundleLock method ReadLock.

public static boolean ReadLock(String bundle) {
    ReentrantReadWriteLock lock = null;
    synchronized (bundleIdentifierMap) {
        lock = bundleIdentifierMap.get(bundle);
        if (lock == null) {
            lock = new ReentrantReadWriteLock();
            bundleIdentifierMap.put(bundle, lock);
        }
    }
    try {
        return lock.readLock().tryLock(3, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        //			e.printStackTrace();
        return false;
    }
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 34 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project atlas by alibaba.

the class BundleLock method WriteLock.

/*
	 * Synchronized lock.
	 */
public static void WriteLock(String bundle) {
    ReentrantReadWriteLock lock = null;
    synchronized (bundleIdentifierMap) {
        lock = bundleIdentifierMap.get(bundle);
        if (lock == null) {
            lock = new ReentrantReadWriteLock();
            bundleIdentifierMap.put(bundle, lock);
        }
    }
    lock.writeLock().lock();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock)

Example 35 with ReentrantReadWriteLock

use of java.util.concurrent.locks.ReentrantReadWriteLock in project perun by CESNET.

the class PerunLocksUtils method lockGroupMembership.

/**
	 * Create transaction locks for combination of group and member (from list of members)
	 * and also bind them to the transaction (as resource by Object uniqueKey)
	 *
	 * @param group the group
	 * @param members list of members
	 * @throws InternalErrorException
	 * @throws InterruptedException
	 */
public static void lockGroupMembership(Group group, List<Member> members) throws InternalErrorException, InterruptedException {
    if (group == null)
        throw new InternalErrorException("Group can't be null when creating lock for group and list of members.");
    if (members == null)
        throw new InternalErrorException("Members can't be null or empty when creating lock for group and list of members.");
    //Sort list of members strictly by ids (there is compareTo method in perunBean using ids for comparing)
    Collections.sort(members);
    List<Lock> returnedLocks = new ArrayList<>();
    try {
        //TODO - On java8 use computeIfAbsent instead
        //try to lock all needed locks there
        ReadWriteLock groupReadWriteLock = groupsLocks.get(group);
        if (groupReadWriteLock == null) {
            groupsLocks.putIfAbsent(group, new ReentrantReadWriteLock(true));
            groupReadWriteLock = groupsLocks.get(group);
        }
        groupReadWriteLock.readLock().lock();
        returnedLocks.add(groupReadWriteLock.readLock());
        for (Member member : members) {
            //Get members lock map by group if exists or create a new one
            ConcurrentHashMap<Member, Lock> membersLocks = groupsMembersLocks.get(group);
            if (membersLocks == null) {
                groupsMembersLocks.putIfAbsent(group, new ConcurrentHashMap<Member, Lock>());
                membersLocks = groupsMembersLocks.get(group);
            }
            //TODO - On java8 use computeIfAbsent instead
            //Get concrete member lock from members lock map or create a new one if not exists
            Lock memberLock = membersLocks.get(member);
            if (memberLock == null) {
                membersLocks.putIfAbsent(member, new ReentrantLock(true));
                memberLock = membersLocks.get(member);
            }
            //Lock the lock and return it
            memberLock.lock();
            returnedLocks.add(memberLock);
        }
        //bind these locks like transaction resource
        if (TransactionSynchronizationManager.getResource(uniqueKey) == null) {
            TransactionSynchronizationManager.bindResource(uniqueKey, returnedLocks);
        } else {
            ((List<Lock>) TransactionSynchronizationManager.getResource(uniqueKey)).addAll(returnedLocks);
        }
    } catch (Exception ex) {
        //if some exception has been thrown, unlock all already locked locks
        unlockAll(returnedLocks);
        throw ex;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Member(cz.metacentrum.perun.core.api.Member) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)52 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)17 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)6 Lock (java.util.concurrent.locks.Lock)5 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 HashMap (java.util.HashMap)4 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)4 Nullable (org.jetbrains.annotations.Nullable)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)2 IOException (java.io.IOException)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 ExecutorService (java.util.concurrent.ExecutorService)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 PostLoad (javax.persistence.PostLoad)2 Configuration (org.apache.hadoop.conf.Configuration)2 Path (org.apache.hadoop.fs.Path)2