Example usage for java.util.concurrent.locks ReadWriteLock writeLock

List of usage examples for java.util.concurrent.locks ReadWriteLock writeLock

Introduction

In this page you can find the example usage for java.util.concurrent.locks ReadWriteLock writeLock.

Prototype

Lock writeLock();

Source Link

Document

Returns the lock used for writing.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.out.println("read to write test");
    ReadWriteLock lock = new ReentrantReadWriteLock();

    lock.readLock().lock(); // get our own read lock
    lock.writeLock().lock(); // upgrade to write lock
    System.out.println("passed");
}

From source file:gridool.util.xfer.RecievedFileWriter.java

private static void releaseLock(final String key, final ReadWriteLock lock,
        final Map<String, ReadWriteLock> locks) {
    lock.writeLock().unlock();
}

From source file:gridool.util.xfer.RecievedFileWriter.java

private static ReadWriteLock accquireLock(final String key, final Map<String, ReadWriteLock> locks) {
    ReadWriteLock lock;
    synchronized (locks) {
        lock = locks.get(key);/*  www  . ja va 2 s.c  o m*/
        if (lock == null) {
            lock = new ReentrantReadWriteLock();
            locks.put(key, lock);
        }
    }
    lock.writeLock().lock();
    return lock;
}

From source file:org.apache.directory.fortress.core.impl.UsoUtil.java

/**
 *
 * @return handle to simple digraph containing user ou hierarchies.
 *///from   w  w w.j av  a2  s .com
private static SimpleDirectedGraph<String, Relationship> getGraph(String contextId) {
    ReadWriteLock hierLock = HierUtil.getLock(contextId, HierUtil.Type.USO);
    String key = getKey(contextId);

    try {
        hierLock.readLock().lock();
        SimpleDirectedGraph<String, Relationship> graph = (SimpleDirectedGraph<String, Relationship>) usoCache
                .get(key);

        if (graph == null) {
            try {
                hierLock.readLock().unlock();
                hierLock.writeLock().lock();

                // TODO: determine why this (code that was commented out) creates a deadlock:
                //graph = ( SimpleDirectedGraph<String, Relationship> ) usoCache.get( key );

                //if ( graph == null )
                //{
                graph = loadGraph(contextId);
                //}
                hierLock.readLock().lock();
            } finally {
                hierLock.writeLock().unlock();
            }
        }

        return graph;
    } finally {
        hierLock.readLock().unlock();
    }
}

From source file:org.apache.directory.fortress.core.impl.PsoUtil.java

/**
 * @param contextId maps to sub-tree in DIT, for example ou=contextId, dc=jts, dc = com.
 * @return handle to simple digraph containing perm ou hierarchies.
 *//* w  ww.j  a  v  a  2s .  c o  m*/
private static SimpleDirectedGraph<String, Relationship> getGraph(String contextId) {
    ReadWriteLock hierLock = HierUtil.getLock(contextId, HierUtil.Type.PSO);
    String key = getKey(contextId);

    try {
        hierLock.readLock().lock();
        SimpleDirectedGraph<String, Relationship> graph = (SimpleDirectedGraph<String, Relationship>) psoCache
                .get(key);

        if (graph == null) {
            try {
                hierLock.readLock().unlock();
                hierLock.writeLock().lock();

                // TODO: determine why this (code that was commented out) creates a deadlock:
                //graph = ( SimpleDirectedGraph<String, Relationship> ) psoCache.get( key );

                //if ( graph == null )
                //{
                graph = loadGraph(contextId);
                //}

                hierLock.readLock().lock();
            } finally {
                hierLock.writeLock().unlock();
            }
        }

        return graph;
    } finally {
        hierLock.readLock().unlock();
    }
}

From source file:org.apache.directory.fortress.core.impl.AdminRoleUtil.java

/**
 * Read this ldap record,{@code cn=Hierarchies, ou=OS-P} into this entity, {@link Hier}, before loading into this collection class,{@code org.jgrapht.graph.SimpleDirectedGraph}
 * using 3rd party lib, <a href="http://www.jgrapht.org/">JGraphT</a>.
 *
 * @param contextId maps to sub-tree in DIT, for example ou=contextId, dc=jts, dc = com.
 * @return handle to simple digraph containing adminRole hierarchies.
 *///from w ww  .  ja  va 2 s.c  o m
private static SimpleDirectedGraph<String, Relationship> getGraph(String contextId) {
    ReadWriteLock hierLock = HierUtil.getLock(contextId, HierUtil.Type.ARLE);
    String key = getKey(contextId);

    try {
        hierLock.readLock().lock();
        SimpleDirectedGraph<String, Relationship> graph = (SimpleDirectedGraph<String, Relationship>) adminRoleCache
                .get(key);

        if (graph == null) {
            try {
                hierLock.readLock().unlock();
                hierLock.writeLock().lock();

                // TODO: determine why this (code that was commented out) creates a deadlock:
                //graph = ( SimpleDirectedGraph<String, Relationship> ) adminRoleCache.get( key );

                //if ( graph == null )
                //{
                graph = loadGraph(contextId);
                //}

                hierLock.readLock().lock();
            } finally {
                hierLock.writeLock().unlock();
            }
        }

        return graph;
    } finally {
        hierLock.readLock().unlock();
    }
}

From source file:org.apache.directory.fortress.core.impl.RoleUtil.java

/**
 *
 * @param contextId maps to sub-tree in DIT, for example ou=contextId, dc=jts, dc = com.
 * @return handle to simple digraph containing role hierarchies.
 *//*from w  w w . j  av a2s . co m*/
private static SimpleDirectedGraph<String, Relationship> getGraph(String contextId) {
    ReadWriteLock hierLock = HierUtil.getLock(contextId, HierUtil.Type.ROLE);
    String key = getKey(contextId);

    try {
        hierLock.readLock().lock();
        SimpleDirectedGraph<String, Relationship> graph = (SimpleDirectedGraph<String, Relationship>) roleCache
                .get(key);

        if (graph == null) {
            try {
                hierLock.readLock().unlock();
                hierLock.writeLock().lock();

                // TODO: determine why this (code that was commented out) creates a deadlock:
                //graph = ( SimpleDirectedGraph<String, Relationship> ) roleCache.get( key );

                //if ( graph == null )
                //{
                graph = loadGraph(contextId);
                //}

                hierLock.readLock().lock();
            } finally {
                hierLock.writeLock().unlock();
            }
        }

        return graph;
    } finally {
        hierLock.readLock().unlock();
    }
}

From source file:RWStripedHashSet.java

/**
 * Constructor/*from   ww  w  . ja v  a2s  .c om*/
 * @param capacity Initial number of  buckets.
 */
public RWStripedHashSet(int capacity) {
    super(capacity);
    locks = new Lock[capacity];
    for (int j = 0; j < locks.length; j++) {
        locks[j] = new ReentrantLock();
    }
    ReadWriteLock rwLock = new ReentrantReadWriteLock();
    readLock = rwLock.readLock();
    writeLock = rwLock.writeLock();
}

From source file:org.alfresco.repo.content.replication.AggregatingContentStore.java

/**
 * Default constructor /* www.  j a  va 2s  .  c  om*/
 */
public AggregatingContentStore() {
    ReadWriteLock storeLock = new ReentrantReadWriteLock();
    readLock = storeLock.readLock();
    writeLock = storeLock.writeLock();
}

From source file:gridool.db.sql.ParallelSQLExecJob.java

private static int invokeCopyIntoTable(@Nonnull final ParallelSQLMapTaskResult result,
        @Nonnull final String outputName, @Nonnull final DBAccessor dba, @Nonnull final LockManager lockMgr,
        @Nonnull final Timings timings) throws GridException {
    final File file = getImportingFile(result, outputName);
    final long filesize = file.length();
    int taskNum = result.getTaskNumber();
    final String tableName = getTaskResultTableName(outputName, taskNum);
    final String sql = constructCopyIntoQuery(file, result, tableName);

    final long mergeTableTime;
    final int affected;
    ReadWriteLock systableLock = lockMgr.obtainLock(DBAccessor.SYS_TABLE_SYMBOL);
    final Lock lock = WORKAROUND_EXLOCK_ON_SYSTBL ? systableLock.writeLock() : systableLock.readLock(); // FIXME REVIEWME why exclusive lock? => sometimes produces wrong result [Trick] read lock for system tables
    final Connection conn = GridDbUtils.getPrimaryDbConnection(dba, true);
    lock.lock();//  ww w.jav a 2 s .co  m
    final long startMergeTableTime = System.currentTimeMillis();
    try {
        affected = JDBCUtils.update(conn, sql);
    } catch (SQLException e) {
        LOG.error(e);
        throw new GridException("failed to execute a query: " + sql, e);
    } finally {
        mergeTableTime = System.currentTimeMillis() - startMergeTableTime;
        lock.unlock();

        JDBCUtils.closeQuietly(conn);
        new FileDeletionThread(file, LOG).start();
    }

    int expected = result.getNumRows();
    if (affected != expected) {
        String warnmsg = "COPY INTO TABLE failed [Expected: " + expected + ", Inserted: " + affected + ']';
        LOG.warn(warnmsg);
        throw new GridException(warnmsg);
    }
    synchronized (timings) {
        timings.mergeTableTimes.add(mergeTableTime);
        timings.recievedBytes.add(filesize);
        timings.recievedRecords.add(affected);
    }
    return affected;
}