Search in sources :

Example 11 with ReentrantReadWriteLock

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

the class ReadWriteLockMultiThreadedTest method testTryWriteLock.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings({ "LockAcquiredButNotSafelyReleased" })
public void testTryWriteLock() throws Exception {
    final ReadWriteLock lock = new ReentrantReadWriteLock();
    lock.readLock().lock();
    X.println("Read lock acquired.");
    IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            boolean res = lock.writeLock().tryLock();
            X.println("Attempting to try write lock: " + res);
            try {
                return null;
            } finally {
                if (res)
                    lock.writeLock().unlock();
            }
        }
    }, 1, "write-lock");
    Thread.sleep(2000);
    X.println("Read lock released: " + lock);
    lock.readLock().unlock();
    fut.get();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with ReentrantReadWriteLock

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

the class TestTDBInternal method exclusive_5.

@Test
public void exclusive_5() {
    DatasetGraph dsg = TDBFactory.createDatasetGraph();
    TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
    ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
    dsg.begin(ReadWrite.READ);
    boolean b = txnmgr.tryExclusiveMode();
    Assert.assertFalse(b);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 13 with ReentrantReadWriteLock

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

the class TestTDBInternal method exclusive_4.

@Test
public void exclusive_4() {
    DatasetGraph dsg = TDBFactory.createDatasetGraph();
    TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
    ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
    checkLock(rwx, 0, 0);
    boolean b = txnmgr.tryExclusiveMode();
    Assert.assertTrue("Exclusive 1", b);
    checkLock(rwx, 0, 1);
    txnmgr.finishExclusiveMode();
    checkLock(rwx, 0, 0);
    b = txnmgr.tryExclusiveMode();
    Assert.assertTrue("Exclusive 2", b);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 14 with ReentrantReadWriteLock

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

the class TestTDBInternal method exclusive_3.

@Test
public void exclusive_3() {
    DatasetGraph dsg = TDBFactory.createDatasetGraph();
    TransactionManager txnmgr = TDBInternal.getTransactionManager(dsg);
    ReentrantReadWriteLock rwx = (ReentrantReadWriteLock) txnmgr.getExclusivityLock$();
    checkLock(rwx, 0, 0);
    txnmgr.startExclusiveMode();
    checkLock(rwx, 0, 1);
    txnmgr.finishExclusiveMode();
    checkLock(rwx, 0, 0);
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 15 with ReentrantReadWriteLock

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

the class OPCPackage method close.

/**
	 * Close the open, writable package and save its content.
	 * 
	 * If your package is open read only, then you should call {@link #revert()}
	 *  when finished with the package.
	 *
	 * @throws IOException
	 *             If an IO exception occur during the saving process.
	 */
@Override
public void close() throws IOException {
    if (this.packageAccess == PackageAccess.READ) {
        logger.log(POILogger.WARN, "The close() method is intended to SAVE a package. This package is open in READ ONLY mode, use the revert() method instead !");
        revert();
        return;
    }
    if (this.contentTypeManager == null) {
        logger.log(POILogger.WARN, "Unable to call close() on a package that hasn't been fully opened yet");
        revert();
        return;
    }
    // Save the content
    ReentrantReadWriteLock l = new ReentrantReadWriteLock();
    try {
        l.writeLock().lock();
        if (this.originalPackagePath != null && !"".equals(this.originalPackagePath.trim())) {
            File targetFile = new File(this.originalPackagePath);
            if (!targetFile.exists() || !(this.originalPackagePath.equalsIgnoreCase(targetFile.getAbsolutePath()))) {
                // Case of a package created from scratch
                save(targetFile);
            } else {
                closeImpl();
            }
        } else if (this.output != null) {
            save(this.output);
            output.close();
        }
    } finally {
        l.writeLock().unlock();
    }
    // Clear
    this.contentTypeManager.clearAll();
}
Also used : ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) File(java.io.File)

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